2017-09-22 7 views
0

ASP.NET MVC를 사용하면 사용자가 일부 노트를 입력하고 즉석에서 저장할 수있게 해주는 TextAreaFor가 포함 된보기가 있습니다. 이전에 저장 한 메모 (다른 사용자 또는 다른 사용자) 또는 기존 메모 수정 (추가 메모 추가). 여기가TextAreaFor에서 텍스트를 저장하는 브라우저 독립적 인 방법

.... 내가 무엇을보기에서 div의의 :

<div class="form-group"> 
     <label for="InternalNotes" class="control-label">Internal Notes</label> 
     @Html.TextAreaFor(w => w.InternalNotes, new { @class = "form-control" , @id="notes" }) @*this is editable*@ 
    </div> 
    <div class="form-group"> 
     <div class="col-xs-6"> 
      <button type="button" id="savenotes" class="btn btn-default btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Save Request Notes</button> 
      <div style="color:green; display:none" id="notessuccess">Notes successfully saved</div> 
      <div style="color:red; display:none" id="noteserror">Notes unable to be saved</div> 
     </div> 
     <div class="col-xs-6 text-right"> 
      <button type="submit" id="deletereq" class="btn btn-default btn-primary" value="delete" name="submit"><span class="glyphicon glyphicon-remove"></span> Delete Request</button> 
     </div> 
    </div> 

그래서 사용자는 TextAreaFor에 뭔가를 입력 할 수 있습니다, 다음 아약스를 통해 그들을 구원해야 "savenotes"버튼을 누르십시오. 다른 사용자가 크롬을 사용하고, 그가 다른 사람을 볼 수 있었다 -

가 내가 원래 사용했지만, 그것은 단지 Internet Explorer에서 일하고 무엇 때문에 "innerText와는"주석
$(document).ready(function() { 

    $("#savenotes").click(function() { 

     $("#notessuccess").hide(); 
     $("#noteserror").hide(); 

     var id = @Model.AccessRequestId; 
     var notes = document.getElementById("notes").textContent; //innerText; 

     $.ajax({ 
      data: { 'id': id, 'notes': notes }, 
      type: 'POST', 
      //contentType: "application/json; charset=utf-8", 
      url: '/Administration/SaveRequestNotes', 
      success: function (data) { 
       if (data.success == "ok") { 
        $("#notessuccess").fadeIn(); 
       } else { 
        $("#noteserror").fadeIn(); 
       } 
      }, 
      fail: function (data) { 
       $("#noteserror").fadeIn(); 
      } 
     }); 
    }); 
}); 

:이은에 대한 jQuery를하다 이미 그곳에 있던 사용자의 노트를 볼 수 있지만, 노트 이외에 노트를 저장하려고하면 노트가 비어있을 수 있습니다.

그래서 "textContent"로 변경했습니다. Internet Explorer에서는 여전히 작동하지만 Chrome 및 Firefox에서는 기존 노트를 비우지 않지만 새로운 노트는 저장하지 않습니다. 브라우저 독립적 인 방법으로이 작업을 수행하여 모든 사람의 메모가 사용중인 모든 내용이 올바르게 저장됩니다.

감사합니다.

답변

0

당신은 jQuery를 val() 방법 당신은 또한 당신의 행동 방식에 대한 올바른 상대 경로를 생성하기 위해 Url.Action 도우미 방법을 고려할 수 있습니다 텍스트 영역

var notes = $("#notes").val(); 
alert(notes); 

에 입력 된 텍스트 사용자를 얻을 수 있습니다.

url: '@Url.Action("SaveRequestNotes","Administration")', 
+0

.val()이 (가) 사용 된 것처럼 보입니다. 왜 그걸 사용하지 않을지 모르겠군요. 나는 그게 어리석은 짓 일 것이라고 생각했습니다! 그냥 내가 지금 가지고있는 것을 사용하는 것보다 @ Url.Action을 사용하는 것이 더 좋은지 궁금하십니까? – Andarta

+0

URL에 관계없이 올바른 상대 경로를 만듭니다. – Shyju