ASP.Net MVC 2 是否可以在 HttpPost 方法中获取在 HttpGet 中传递的相同模型实例(稍作更改)

发布于 2024-08-31 13:29:08 字数 2699 浏览 3 评论 0原文

我有一个复杂的实体 User:

    public class User : BaseEntity
    {        
    public virtual Taxi Taxi { get; set; }  --> That is why i call it "complex"
    public virtual string Login { get; set; }           
    public virtual string Password { get; set; }  
    }

其中 Taxi 是 User 的父级(Taxi 有许多用户):

    public class Taxi : BaseEntity
    {
      public virtual string Name { get;  set; }
      public virtual string ClientIp { get;  set; }
    }

BaseEntity 由 public virtual int Id { get; 组成;私人套装;尝试

时出现问题

    [Authorize]  
    public ActionResult ChangeAccountInfo()
    {
        var user = UserRepository.GetUser(User.Identity.Name);
        return View(user); 
    }

编辑 User My ChangeAccountInfo.aspx

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

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

         <div class="editor-field">
            <%: Html.HiddenFor(model => model.Taxi.Name)%>               
        </div>     

        <p>
            <input type="submit" value="Save" />
        </p>
       </fieldset>

发布更改

    [Authorize]
    [HttpPost]
    public ActionResult ChangeAccountInfo(User model)
    {
        if (ModelState.IsValid)
        {
            UserRepository.UpdateUser(model); 

            return RedirectToAction("ChangeAccountInfoSuccess", "Account");
        }

        return View(model);
    }

:但是,(用户模型)参数已 用户.Id == 0 -->用户实体在编辑前有 5
User.Login ==“我的新登录名”
User.Password == "我的新密码"
User.Taxi.Id == 0 --> User.Taxi 实体在编辑前有 3
User.Taxi.Name == "旧的隐藏名称"
User.Taxi.ClientIp == null -->用户实体在编辑之前有 192.168.0.1

Q: 是否可以不使用标签“隐藏”来标记实体的所有字段(应该在我的 UpdateUser 中),但在我的 HttpPost 方法中仍然保持它们不变? 例如,不是 User.Taxi.ClientIp = null,而是 User.Taxi.ClientIp = 192.168.0.1

如果重要的话,我正在使用 nhibernate。

I have a complex entity User:

    public class User : BaseEntity
    {        
    public virtual Taxi Taxi { get; set; }  --> That is why i call it "complex"
    public virtual string Login { get; set; }           
    public virtual string Password { get; set; }  
    }

where Taxi is a parent of User (Taxi has-many Users):

    public class Taxi : BaseEntity
    {
      public virtual string Name { get;  set; }
      public virtual string ClientIp { get;  set; }
    }

BaseEntity consists of public virtual int Id { get; private set; }

The problem occurs while trying to edit User

    [Authorize]  
    public ActionResult ChangeAccountInfo()
    {
        var user = UserRepository.GetUser(User.Identity.Name);
        return View(user); 
    }

My ChangeAccountInfo.aspx

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

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

         <div class="editor-field">
            <%: Html.HiddenFor(model => model.Taxi.Name)%>               
        </div>     

        <p>
            <input type="submit" value="Save" />
        </p>
       </fieldset>

Post changes:

    [Authorize]
    [HttpPost]
    public ActionResult ChangeAccountInfo(User model)
    {
        if (ModelState.IsValid)
        {
            UserRepository.UpdateUser(model); 

            return RedirectToAction("ChangeAccountInfoSuccess", "Account");
        }

        return View(model);
    }

But, the (User model) parameter has
User.Id == 0 --> User entity had 5 before edit
User.Login == "my new login"
User.Password == "my new password"
User.Taxi.Id == 0 --> User.Taxi entity had 3 before edit
User.Taxi.Name == "old hidden name"
User.Taxi.ClientIp == null --> User entity had 192.168.0.1 before edit

Q:
Is it possible not to mark all the fields of an entity (that should be in my UpdateUser) with tag "hidden" but still have them unchanged in my HttpPost method?
e.g. not User.Taxi.ClientIp = null, but User.Taxi.ClientIp = 192.168.0.1

I'm using nhibernate, if it matters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执手闯天涯 2024-09-07 13:29:08

并非没有一些繁重的工作。我不确定 nhibernate 是否关心它是否是同一个实例;您可能只需要保留实体的 ID 即可使表单正常工作。

如果第二种情况成立,您所需要做的就是在表单中创建一个隐藏字段来存储模型的 id。 MVC 将完成剩下的工作。只需将其放在顶部的表单中即可:

<%= Html.HiddenFor(model => model.Id) %>

如果您担心人们黑客攻击(并且您应该这样做),您可以指定(通过白名单或黑名单)表单中可以/不能编辑哪些属性。

Not without some heavy lifting. I'm not sure if nhibernate cares that it is the same exact instance or not; you might only have to keep the entity's ID for your form to work.

If the second case is true, all you need to do is create a hidden field in your form to store the id of the model. MVC will do the rest. Just chuck this in your form at the top:

<%= Html.HiddenFor(model => model.Id) %>

You can specify (via a whitelist or a blacklist) what properties can/cannot be edited within the form, if you're concerned about people hacking (and you should be).

不再让梦枯萎 2024-09-07 13:29:08

威尔建议的答案最符合我的问题:
编辑您的实体
- 为您的视图提供您要编辑的实体标识
- 在隐藏字段中发布带有身份的模型(在我的情况下,由于 nhib 映射设置,我无法使用 model.Id 私人设置器)
- 在httpPost方法中使用GetById(idFromHiddenField)来检索您的entityFromDatabase
- 使用 UpdateModel(entityFromDatabase) - 这将合并旧版本(entityFromDatabase) 和新版本的实体
- 然后 ISession.Update(entityFromDatabase) 将更改保存到数据库中

The answer suggested by Will is the best matching my issue:
To Edit your entity
- Provide your View with entity identity that you want to edit
- Post your model with identity in hidden field (in my case i can't use model.Id private setter because of nhib mapping settings)
- In httpPost method use GetById(idFromHiddenField) to retrieve your entityFromDatabase
- use UpdateModel(entityFromDatabase) - that will merge old version(entityFromDatabase) and new version of entity
- Then ISession.Update(entityFromDatabase) to persist changes into database

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文