自定义视图模型与MVC 2强类型HTML辅助返回上创建空的对象? [英] Custom ViewModel with MVC 2 Strongly Typed HTML Helpers return null object on Create?
问题描述
我有一个麻烦,而试图创建一个模型自定义视图的实体创建表单。下面是我对分类创建表单自定义视图模式。
I am having a trouble while trying to create an entity with a custom view modeled create form. Below is my custom view model for Category Creation form.
public class CategoryFormViewModel
{
public CategoryFormViewModel(Category category, string actionTitle)
{
Category = category;
ActionTitle = actionTitle;
}
public Category Category { get; private set; }
public string ActionTitle { get; private set; }
}
这是我的用户控制,其中用户界面是
and this is my user control where the UI is
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CategoryFormViewModel>" %>
<h2>
<span><%= Html.Encode(Model.ActionTitle) %></span>
</h2>
<%=Html.ValidationSummary() %>
<% using (Html.BeginForm()) {%>
<p>
<span class="bold block">Başlık:</span>
<%=Html.TextBoxFor(model => Model.Category.Title, new { @class = "width80 txt-base" })%>
</p>
<p>
<span class="bold block">Sıra Numarası:</span>
<%=Html.TextBoxFor(model => Model.Category.OrderNo, new { @class = "width10 txt-base" })%>
</p>
<p>
<input type="submit" class="btn-admin cursorPointer" value="Save" />
</p>
<% } %>
当我点击保存按钮,它不因为我使用的自定义视图模型绑定我的类别和强类型HTML辅助方法一样,
When i click on save button, it doesnt bind the category for me because of i am using custom view model and strongly typed html helpers like that
<%=Html.TextBoxFor(model => Model.Category.OrderNo) %>
我的HTML源代码看起来像这样
My html source looks like this
<form action="/Admin/Categories/Create" method="post">
<p>
<span class="bold block">Başlık:</span>
<input class="width80 txt-base" id="Category_Title" name="Category.Title" type="text" value="" />
</p>
<p>
<span class="bold block">Sıra Numarası:</span>
<input class="width10 txt-base" id="Category_OrderNo" name="Category.OrderNo" type="text" value="" />
</p>
<p>
<input type="submit" class="btn-admin cursorPointer" value="Kaydet" />
</p>
</form>
我怎样才能解决这个问题?
How can i fix this?
推荐答案
您查看模型需要不带参数的默认构造函数,你需要为每个属性的公共set方法。默认的模型绑定使用公共setter方法来填充对象。
Your View Model needs a default constructor without parameters and you need public set methods for each of the properties. The default model binder uses the public setters to populate the object.
默认的模型粘合剂有一些它遵循的规则。它选择哪些数据绑定到以下顺序:
The default model binder has some rules it follows. It chooses what data to bind to in the following order:
- 从后表单参数
- 将您的路由定义中的global.asax.cs定义的URL路由数据
- 查询字符串参数
默认的模型绑定,然后使用一些策略来绑定模型/在动作方法的参数:
The default model binder then uses several strategies to bind to models/parameters in your action methods:
- 完全名称匹配
- 以prefix.name,其中preFIX是父类和名称是子类/属性相匹配
- 无preFIX(只要没有冲突,你不必担心提供preFIX)名称
您可以覆盖从绑定属性的几个选项的行为。这些措施包括:
You can override the behavior with several options from the Bind attribute. These include:
- [装订(preFIX =一些preFIX)] - 强制地图由preFIX确定一个特定的父类
- [绑定(包括=VAL1,将val2)] - 名字的白名单绑定到
- [绑定(不包括=VAL1,将val2)] - 名从默认的行为排除
- [Bind(Prefix = "someprefix")] -- Forces a map to a specific parent class identified by the prefix.
- [Bind(Include = "val1, val2")] -- Whitelist of names to bind to
- [Bind(Exclude = "val1, val2")] -- Names to exclude from default behavior
这篇关于自定义视图模型与MVC 2强类型HTML辅助返回上创建空的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!