我已将模型绑定到视图,模型中的每个属性在视图中都有一个关联的控件。
这是一个很大的模型,视图上有多个控件。用户更新视图中的数据,当他单击“保存”按钮时,应将更新的模型值发送到控制器操作方法。这里我正在对控制器方法进行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);
}
}
}
答案 0 :(得分:0)
您正在使用(Html.BeginForm()),因此您可以使用此代码
有一个功能正是这样做的:
http://api.jquery.com/serialize/
var data = $('form').serialize();
$.post('url', data);