asp.net-MVC2 - 不使用型号强类型的辅助? [英] asp.net-mvc2 - Strongly typed helpers not using Model?

查看:171
本文介绍了asp.net-MVC2 - 不使用型号强类型的辅助?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在使用MVC2强类型佣工输入字段值不会从Model属性在一个职位而采取的。这是默认的行为?

(强类型)查看使用强类型助手:

 < D​​IV CLASS =编辑标记>
    <%:Html.LabelFor(型号=> model.Name)%GT;
< / DIV>
< D​​IV CLASS =主编场>
    <%:Html.TextBoxFor(型号=> model.Name)%GT;
    <%:Html.ValidationMessageFor(型号=> model.Name)%GT;
< / DIV>< D​​IV CLASS =编辑标记>
    <%:Html.LabelFor(型号=> model.Price)%GT;
< / DIV>
< D​​IV CLASS =主编场>
    <%:Html.TextBoxFor(型号=> model.Price)%GT;
    <%:Html.ValidationMessageFor(型号=> model.Price)%GT;
< / DIV>

控制器动作:/产品/编辑/ 5

 公众的ActionResult编辑(INT ID)
    {
        变种p值=新产品();
        p.Name =产品1;
        p.Price =100;
        返回查看(P);
    }

HTML输出:

 < D​​IV CLASS =编辑标记>
    <标签=姓名>名称和LT; /标签>
< / DIV>< D​​IV CLASS =主编场>
    <输入ID =名称NAME =名称TYPE =文本VALUE =物1/>
< / DIV>< D​​IV CLASS =编辑标记>
    <标签=价格>价格< /标签>
< / DIV>
< D​​IV CLASS =主编场>
    <输入ID =价格NAME =价格型=文本VALUE =100/>
< / DIV>

控制器动作:/产品/编辑/ 5

  [HttpPost]
    公众的ActionResult编辑(产品P)
    {
        p.Name =prrrrrrd 2;
        返回查看(P);    }

HTML表单后,后输出(下文我希望使用id =名称输入的值为prrrrrrd 2.哪里强类型的帮手得到它的值从?):

 < D​​IV CLASS =编辑标记>
    <标签=姓名>名称和LT; /标签>
< / DIV>< D​​IV CLASS =主编场>
    <输入ID =名称NAME =名称TYPE =文本VALUE =物1/>
< / DIV>< D​​IV CLASS =编辑标记>
    <标签=价格>价格< /标签>
< / DIV>
< D​​IV CLASS =主编场>
    <输入ID =价格NAME =价格型=文本VALUE =100/>
< / DIV>


解决方案

  

当在使用MVC2强类型的帮手输入字段值
  当柱子是没有从Model属性服用。这是
  默认的行为?


是的,他们首先从ModelState中取,然后从模型。如果你打算在你的POST操作执行对模型进行一些修改,你需要首先从ModelState中删除它们。例如:

  [HttpPost]
公众的ActionResult编辑(产品P)
{
    ModelState.Remove(姓名);
    p.Name =prrrrrrd 2;
    返回查看(P);
}

When using strongly typed helpers in MVC2 the input field values aren't taken from the Model property when a post is made. Is this default behavior?

(strongly typed) view with strongly typed helpers:

<div class="editor-label">
    <%: Html.LabelFor(model => model.Name) %>
</div>
<div class="editor-field">
    <%: Html.TextBoxFor(model => model.Name) %>
    <%: Html.ValidationMessageFor(model => model.Name) %>
</div>

<div class="editor-label">
    <%: Html.LabelFor(model => model.Price) %>
</div>
<div class="editor-field">
    <%: Html.TextBoxFor(model => model.Price) %>
    <%: Html.ValidationMessageFor(model => model.Price) %>
</div>

Controller action for: /Product/Edit/5

    public ActionResult Edit(int id)
    {
        var p = new Product();
        p.Name = "product 1";
        p.Price = "100";
        return View(p);
    }

Html output:

<div class="editor-label">
    <label for="Name">Name</label>
</div>

<div class="editor-field">
    <input id="Name" name="Name" type="text" value="product 1" />
</div>

<div class="editor-label">
    <label for="Price">Price</label>
</div>
<div class="editor-field">
    <input id="Price" name="Price" type="text" value="100" />
</div>

Controller action for: /Product/Edit/5

    [HttpPost]
    public ActionResult Edit(Product p)
    {
        p.Name = "prrrrrrd 2";
        return View(p);

    }

Html output after form post (below I would expect the value of the input with id="Name" to be "prrrrrrd 2. Where does the strongly typed helper get it's value from?):

<div class="editor-label">
    <label for="Name">Name</label>
</div>

<div class="editor-field">
    <input id="Name" name="Name" type="text" value="product 1" />
</div>

<div class="editor-label">
    <label for="Price">Price</label>
</div>
<div class="editor-field">
    <input id="Price" name="Price" type="text" value="100" />
</div>

解决方案

When using strongly typed helpers in MVC2 the input field values aren't taken from the Model property when a post is made. Is this default behavior?

Yes, they are first taken from the ModelState and then from the Model. If you intend to perform some modifications on the model in your POST action you need to remove them from the ModelState first. For example:

[HttpPost]
public ActionResult Edit(Product p)
{
    ModelState.Remove("Name");
    p.Name = "prrrrrrd 2";
    return View(p);
}

这篇关于asp.net-MVC2 - 不使用型号强类型的辅助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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