这个问题已经被问了很多次,但我找不到一个令人满意的答案。我在类似的帖子上尝试了所有的建议,但都没有成功。
当我使用Ajax.BeginForm时,它不会将我更新的值发送到控制器。它始终是默认的ViewModel。
//Viewmodel
public class MyViewModel
{
[Required]
public int Prop1 {get; set;}
[Required]
public int Prop2 {get; set;}
}
//Controller action
[HttpPost]
public ActionResult Test(MyViewModel model)
{
return PartialView("_partialview");
}
//View
@model Namespace.MyViewModel;
@using(Ajax.BeginForm("Test", Model, new AjaxOptions()
{
UpdateTargetId = "divName",
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace
}))
{
@Html.TextBoxFor(x => x.Prop1)
@Html.TextBoxFor(x => x.Prop2)
<button type="submit">Submit</button>
}我尝试过多个建议,但控制器内部发布的值始终是没有更新值的默认MyViewModel。
我还尝试了不同的变体来发布这些值:
@using(Ajax.BeginForm("Test", new { model }, new AjaxOptions()
@using(Ajax.BeginForm("Test", new { model = @Model }, new AjaxOptions()
@using(Ajax.BeginForm("Test", new { model =
new Namespace.MyViewModel{ Prop1 = @Model.Prop1, Prop2 = @Model.Prop2 } }, new AjaxOptions()使用的捆绑包:
"~/bundles/jqueryval"
"~/Scripts/jquery.validate*",
"~/Scripts/jquery.unobtrusive-*"我真的不知道还可以尝试什么来张贴更新的值。
发布于 2017-10-17 22:42:46
嗨,我用这个解决了这个问题:
将"id“放入方法中
public ActionResult StolenBadge(StolenDocViewModel id)
{
var obj = id
... rest of code ...
}在Ajax表单中等于"id = Model“
@model WFAccess.Models.ViewModels.StolenDocViewModel
@using (Ajax.BeginForm("StolenBadge", "Requests", new AjaxOptions()
{
HttpMethod = "POST",
UpdateTargetId = "document-body",
InsertionMode = InsertionMode.Replace,
LoadingElementId = "document-loading",
OnBegin = "ClearBody('document-body')"
}, new { id = Model }))
{
... rest of form ...
}https://stackoverflow.com/questions/28396234
复制相似问题