2017-01-17 7 views
0

モデルをビューにバインドしました。モデル内の各プロパティにビュー内の関連するコントロールがあります。Jqueryで厳密に型指定されたASP.Net MVCモデル値にアクセス

大きなモデルで、ビューには複数のコントロールがあります。ユーザーはビュー内のデータを更新し、[保存]ボタンをクリックすると、更新されたモデル値がコントローラーアクションメソッドに送信されます。ここではAJAXをコントローラメソッドに呼び出します。

これは厳密に型指定されたビューであるため、コントロール値にアクセスする代わりに、更新されたモデルを直接渡す可能性があるかどうかを確認しています。

詳細情報が必要な場合はお知らせください。私が試したPFBコード

おかげで、 バーラト

ビューコード

@model WebApplication1.Models.PersonModel 

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 


@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>PersonModel</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.DateTime, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.DateTime, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.DateTime, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Age, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Company, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Company, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Company, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Save" id="btnGet" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     //window.setInterval(Setinterval, 10000); 

      $("#btnGet").click(function() { 
       Setinterval(); 
      }); 

      function Setinterval() { 
       //var request = new PersonModel1(); 
       //var request = '<%= @Model %>';/// '<%= Model %>'; 
       var request = @Html.Raw(Json.Encode(Model)); 
       $.ajax({ 

        url: "/ST/SubmitRequest", 
        dataType: "json", 
        contentType: "application/json", 
        type: "POST", 
        data: JSON.stringify(request), 
        success: function (response) { 
         //Setinterval(); 
         alert("Done...!"); 
        }, 
        failure: function (response) { 
         alert(response.responseText); 
        }, 
        error: function (response) { 
         alert(response.responseText); 
        } 
       }); 
      }; 

     }); 


</script> 

コントローラーコード

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using WebApplication1.Models; 

namespace WebApplication1.Controllers 
{ 
    public class STController : Controller 
    { 
     // GET: ST 
     public ActionResult Index() 
     { 
      PersonModel pm = new PersonModel() { 
       Age = "34", 
       Company = "DDDD", 
       DateTime = DateTime.Now, 
       Name = "XYZ S" 
      }; 
      return View(pm); 
     } 

     [HttpPost] 
     public JsonResult SubmitRequest(PersonModel pm) 
     { 

      return Json(pm); 
     } 
    } 
} 
+1

'VAR要求= @ Html.Raw(Json.Encode(モデル));'元のモデルではなく、編集した値です。 'data:$( 'form')を使ってください。serialize()'と 'contentType:" application/json "を削除してください、 –

+0

見てくださいhttp://stackoverflow.com/a/8539918/1530987 – crowchirp

+0

ありがとう@StephenMuecke。出来た。 –

答えて

0

あなたは(Html.BeginForm())を使用しているので、あなたはこれを使用することができますコード

まさにこれを行う機能があります:

http://api.jquery.com/serialize/

var data = $('form').serialize(); 
$.post('url', data); 
関連する問題