2017-01-17 56 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

'变种请求= @ 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);