[英]Model is not updated with ajax post in asp.net mvc?
I am using asp.net mvc application. 我正在使用asp.net mvc应用程序。 In my view page i have textbox like below code:
在我的查看页面中,我有如下代码所示的文本框:
@Html.TextBoxFor(m => m.Id, new { @class = "form-control", @readonly = "readonly" })
Also, in form submit ajax post, 另外,在表单中提交ajax帖子,
$('#btnSubmit').click(function(e)
{
$.ajax({
url: '/button/Button',
type: 'POST',
data: {Id:1},
});
});
My controller: 我的控制器:
MasterM empcode = new MasterM();
[HttpGet]
public ActionResult Button()
{
var empcode = new MasterM();
return View(empcode);
}
[HttpPost]
public ActionResult Button(MasterM model)
{
ModelState.Clear();
if (!ModelState.IsValid)
{
return View(model);
}
return View(model);
}
after this form submit the value of the text box should change to new value (1). 此表单提交后,文本框的值应更改为新值(1)。 How can i achieve this in controller section itself.
我如何在控制器部分本身中实现这一目标。 I know in ajax success we can manually update the element value but i want to achieve this in controller section and update model with new values.
我知道ajax成功后,我们可以手动更新元素值,但是我想在控制器部分实现这一点,并使用新值更新模型。
When you are Posting your are sending only a Single Id and not the Entire Model , So basically this 当您发布时,您只发送一个ID,而不发送整个模型,因此基本上
public ActionResult Button(MasterM model) will be empty
and since you are returning back the empty set the text box will not be updated. 并且由于您要返回空集,因此不会更新该文本框。
There are 2 things you can do here : 1) Modify the Receiving type to 您可以在此处执行以下两项操作:1)将接收类型修改为
Public ActionResult Button(int Id)
{
MasterM model = new MasterM();
model.Id = Id;
if (!ModelState.IsValid)
{
return View(model);
}
return View(model);
}
2) If you want to retain Model as your Param in Controller you need to serialize the Data in Jquery and post it . 2)如果要将Model保留为Controller中的参数,则需要在Jquery中序列化数据并将其发布。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.