查看数据字典中的ASP.NET MVC覆盖模型数据 [英] View data dictionary overriding model data in ASP.NET MVC

查看:113
本文介绍了查看数据字典中的ASP.NET MVC覆盖模型数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图中创建一个用户,如下所示。

I have a view to create a user as follows.

<% using (Html.BeginForm("SaveUser", "Security")) {%>
    <p>
        <label for="UserName">UserName:</label>
        <%= Html.TextBox("UserName") %>
        <%= Html.ValidationMessage("UserName", "*") %>
    </p>
    <p>
        <label for="Password">Password:</label>
        <%= Html.TextBox("Password") %>
        <%= Html.ValidationMessage("Password", "*") %>
    </p>
    <p>
        <input type="submit" value="Create" />
    </p>
<}%>

在单击创建按钮,HTML表单发布到被称为SaveUser的操作,接受了POST动词只如下:

When the "Create" button is clicked, the HTML form is posted to an action called "SaveUser" that accepts the "POST" verb only as follows.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveUser( UserViewModel user)
{
    //user.Id is zero before save
    //Save the user.  Code omitted...
    //user.Id is now greater than zero
    //redirect to edit user view
    return View("EditUser", user );
}

用户保存后,页面被重定向到EditUser视图以

After the user is saved, the page is redirected to the "EditUser" view with

<p>
    <label for="Id">Id:</label>
    <%= Html.Hidden("Id", Model.Id)%>
</p>

下面的问题是:为隐藏字段的值保持显示为零点虽然。 Model.Id 大于零。它似乎是别的东西覆盖模型视图值。 ViewDataDictonary 是嫌疑人。所以路线如下的动作返回视图之前补充说。

Here is the problem: the value for the hidden field kept showing up as zero though. Model.Id is greater than zero. It seemed that something else is overriding the model view value. ViewDataDictonary was a suspect. So a line is added before returning the view in the action as follows.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveUser( UserViewModel user)
{
    //user.Id is zero before save
    //Save the user.  Code omitted...
    //user.Id is now greater than zero

    //clear the view data
    ViewData = new ViewDataDictionary();
    //redirect to edit user view
    return View( "EditUser", user);
}

果然,这个工作。隐藏字段现在有正确的用户ID的值。

Sure enough, this worked. The hidden field now has a value of the correct user ID.

我们找到了一种方法来治疗的症状,但如果是问题的根源?

我不喜欢每次结算视图数据字典回来的另一种观点认为之前的想法。

I don't like the idea of clearing view data dictionary every time before a returning another view.

推荐答案

成功操作后,你应该使用

After successful operation you should use

return RedirectToAction("EditUser", new { id = user.Id });

或类似code。目前的ModelState是用来生成视图和模型粘合剂没有绑定标识。

or similar code. The current ModelState is used to generate view and model binder didn't bind Id.

[绑定(不包括=ID)] 也可以工作,但重定向创建新页面(不使用当前的ModelState),是一个更好的解决方案。

[Bind(Exclude = "Id")] could also work, but the redirection creates new page (not using current ModelState) and is a better solution.

编辑:

如果您不想绑定整个对象,你应该使用 [绑定(不含)] 或你应该只定义 SaveUser SaveUser(用户名字符串,字符串密码)和建 UserViewModel 反对自己。这将节省您从模型粘结剂和模型值产生的错误,你不知道从哪里来的。

If you don't want to bind whole object, you should use [Bind (Exclude)] or you should just define SaveUser as SaveUser(string userName, string password) and build the UserViewModel object yourself. This will save you from errors generated by model binder and Model values, that you don't know where come from.

这篇关于查看数据字典中的ASP.NET MVC覆盖模型数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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