BeginForm在ChildAction使用错误的ID [英] BeginForm in ChildAction uses wrong id

查看:136
本文介绍了BeginForm在ChildAction使用错误的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有是一些简单的我不明白ChildActions

我已经创建了一个模型,加载与窗体孩子动作的简单视图。
孩子动作有另一种模式比其父,用不同的 ID 属性。

Html.HiddenFor(M = GT; m.Id)输出仍然在家长 ID ,虽然 @ Model.id 输出正确的值!

我不能可靠地使用在ChildActions的辅助方法,或者这是一个已知的bug?

的HomeController

 公共类HomeController的:控制器
{
    公众的ActionResult指数()
    {
        VAR模型=新Models.HomeModel {ID = 1,消息=bugmodel};
        返回查看(模型);
    }    [HTTPGET]
    [ChildActionOnly]
    公众的ActionResult儿童(INT ID)
    {
        VAR模型=新Models.HomeChildModel {ID = 100,parentId的= ID,childMessage =我的孩子的消息};
        返回PartialView(模型);
    }    [HttpPost]
    [ActionName(子)]
    [ValidateAntiForgeryToken()]
    公众的ActionResult ChildPost(Models.HomeChildModel模型)
    {
        返回RedirectToAction(「指数」);
    }
}

模式

 公共类HomeModel
{
    公众诠释ID {搞定;组; }
    公共字符串消息{搞定;组; }
}公共类HomeChildModel
{
    公众诠释ID {搞定;组; }
    公众诠释parentId的{搞定;组; }
    公共字符串childMessage {搞定;组; }
}

主页视图

  @model ChildActionBug.Models.HomeModel
@ {
    ViewBag.Title =指数;
}< H2>指数< / H>
@ Html.DisplayFor(M = GT; m.id)
@ Html.DisplayFor(M = GT; m.message)@ Html.Action(孩子,新{ID = Model.id})**子视图**@model ChildActionBug.Models.HomeChildModel
< H3>儿童此处< / H3 GT&;
@using(Html.BeginForm())
{
    @ Html.AntiForgeryToken()
    @ Html.HiddenFor(M = GT; m.id)
    @ Html.HiddenFor(M = GT; m.parentId)
    @ Html.EditorFor(M = GT; m.childMessage)    < D​​IV>儿童型号ID:@ Model.id< / DIV>
    <按钮式=提交>保存< /按钮>
}


解决方案

根据在SO问题我张贴在评论中给出的答案,你最好明确创建隐藏字段

<一个href=\"http://stackoverflow.com/questions/4710447/asp-net-mvc-html-hiddenfor-with-wrong-value\">ASP.Net MVC Html.HiddenFor与错误的值


  

这是正常的,这是HTML佣工的工作方式。他们先用
  POST请求模型中的值的和之后的值。这个
  也就是说,即使你修改模型的价值你
  控制器动作如果在POST请求相同的变量
  您的修改将被忽略,张贴的值会被使用。


所以取而代之,纯手工工艺的隐藏字段:

 &LT;输入类型=隐藏的名字=ID值=@ Model.Id/&GT;
&LT;输入类型=隐藏的名字=的ParentIdVALUE =@ Model.ParentId/&GT;
&LT;输入类型=隐藏的名字=ChildMessageVALUE =@ Model.ChildMessage/&GT;

There is something simple I don't understand with ChildActions.

I've created a simple View for a model, that loads a child action with a form. The child action has another model than its parent, with a different id property.

Html.HiddenFor(m => m.Id) still outputs the parents id, although @Model.id outputs the correct value!

Can't I reliably use the Helper methods in ChildActions, or is this a known bug?

HomeController

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new Models.HomeModel { id = 1, message = "bugmodel" };
        return View(model);
    }

    [HttpGet]
    [ChildActionOnly]
    public ActionResult Child(int id)
    {
        var model = new Models.HomeChildModel { id = 100, parentId = id, childMessage = "My Child message" };
        return PartialView(model);
    }

    [HttpPost]
    [ActionName("Child")]
    [ValidateAntiForgeryToken()]
    public ActionResult ChildPost(Models.HomeChildModel model)
    {
        return RedirectToAction("Index");
    }
}

Models

public class HomeModel
{
    public int id { get; set; }
    public string message { get; set; }
}

public class HomeChildModel
{
    public int id { get; set; }
    public int parentId { get; set; }
    public string childMessage { get; set; }
}

Home view

@model ChildActionBug.Models.HomeModel
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@Html.DisplayFor(m=>m.id)
@Html.DisplayFor(m=>m.message)

@Html.Action("Child", new { id = Model.id })

**Child view**

@model ChildActionBug.Models.HomeChildModel
<h3>Child here</h3>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(m=>m.id)
    @Html.HiddenFor(m=>m.parentId)
    @Html.EditorFor(m=>m.childMessage)

    <div>Child Model ID: @Model.id</div>
    <button type="submit">Save</button>
}

解决方案

Based on the answer given in the SO question I posted in the comment, you're better off explicitly creating the hidden fields

ASP.Net MVC Html.HiddenFor with wrong value

That's normal and it is how HTML helpers work. They first use the value of the POST request and after that the value in the model. This means that even if you modify the value of the model in your controller action if there is the same variable in the POST request your modification will be ignored and the POSTed value will be used.

So instead, hand craft the hidden fields:

<input type="hidden" name="Id" value="@Model.Id" />
<input type="hidden" name="ParentId" value="@Model.ParentId" />
<input type="hidden" name="ChildMessage" value="@Model.ChildMessage" />

这篇关于BeginForm在ChildAction使用错误的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆