ASP.NET MVC 2模型绑定部分 [英] ASP.NET MVC 2 Model partial binding

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

问题描述

我的问题是,我想部分编辑我的模型 - 显示2场和编辑,例如另外2场。当POST发生,返回模型只包含,我只用它来显示为NULL编辑,其他领域的领域。如何解决这个问题,在POST要返回模型各个领域,因为错误,当我回到这个模型和领域为NULL也不是那么好?

My problem is that I want to partially edit my model - to display 2 fields and to edit another 2 fields for example. When POST happens, returned model contains only the fields that are editable, other fields that I use only to display are NULL. How to fix this, on POST to be returned model with all fields, because on ERROR when I return this model and fields are NULL is not so good?

推荐答案

该模型粘结剂结合的形式价值只有具有二传手模型属性。

The model binder binds the form values only to model properties that have a setter.

根据你需要达到什么:

您可以使用隐藏的输入存储在视图中的值,这些必将回来(因为该属性有一个setter)

You can use hidden inputs to store the values in the view, these will be bound back (given that the properties have a setter)

<%= Html.Hidden(Model.SomeField) %>

class YourViewModel
{
    public SomeField {get; set;}

或者,你应该确保在你的控制器动作,你只更新您在视图中显示的字段,不是空的。

Or you should ensure in your controller action that you are only updating the fields that you displayed in the view, not the null ones.

在理想情况下,您的视图模型应该仅包含相关的视图(以及由控制器处理的逻辑)。

Ideally, your viewmodel should contain only the properties that are relevant for the view (and for the logic handled by the controller).

这篇关于ASP.NET MVC 2模型绑定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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