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);
}
}
}
'變種請求= @ Html.Raw(Json.Encode(型號));'是原始模型,而不是編輯的值。使用'data:$('form')。serialize()'並移除'contentType:「application/json」,' –
請看看http://stackoverflow.com/a/8539918/1530987 – crowchirp
謝謝@StephenMuecke。有效。 –