我应该如何使用这个简单的为 ASP.NET MVC3 视图创建视图来编程该控制器 ActionResult 方法?
我不确定是否应该使用 FormsCollection
或其他东西来处理这个基本的 Create
视图。
(注意:我正在使用 标签的自定义编辑器模板ICollection
)
模型
public class Question
{
[Required, StringLength(100)]
public string Title { get; set; }
[Required]
public string Body { get; set; }
public ICollection<string> Tags { get; set; }
}
视图
@model AWing.Core.Entities.Product
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Question</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Tags)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Tags, "tags")
@Html.ValidationMessageFor(model => model.Tags)
</div>
<p>
<input type="submit" value="Create a new product" />
</p>
</fieldset>
}
控制器
[HttpPost]
public ActionResult Create(FormsCollection formsCollection)
{
... ????
}
或其他什么?
[HttpPost]
public ActionResult Create(... ??? ...)
{
.... ????
}
I'm not sure if I should use a FormsCollection
or something else, with this basic Create
view.
(note: I'm using a custom editor template for the Tags ICollection<string>
)
Model
public class Question
{
[Required, StringLength(100)]
public string Title { get; set; }
[Required]
public string Body { get; set; }
public ICollection<string> Tags { get; set; }
}
View
@model AWing.Core.Entities.Product
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Question</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Tags)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Tags, "tags")
@Html.ValidationMessageFor(model => model.Tags)
</div>
<p>
<input type="submit" value="Create a new product" />
</p>
</fieldset>
}
Controller
[HttpPost]
public ActionResult Create(FormsCollection formsCollection)
{
... ????
}
or something else?
[HttpPost]
public ActionResult Create(... ??? ...)
{
.... ????
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您有一个强类型视图,因此您可以让您的
[HttpPost]
方法采用与变量相同的类型,例如:DefaultModelBinder
将采用返回的值,并插入将它们放入您的强类型模型中。中提琴!只要标签集合模板的模型是
string
类型,MVC 就会迭代该集合,以便可以绑定它(尽管您可能必须从ICollection< /code> 到列表)。
更新
正如我们在评论中讨论的,不要为每个标签创建一个文本框,而是创建一个单独的 ViewModel,其中包含所有其他产品属性。
而不是使用
List;在 ViewModel 中的 Tags
中,创建此属性:在您的视图中,有一个
TagCollection
文本框。然后,在 Create 操作中,您可以将 TagCollection 字符串解析为标签列表。Since you have a strongly typed View, you can have your
[HttpPost]
method take that same type as the variable, e.g.:The
DefaultModelBinder
will take the values returned, and plug them into your strongly typed model. Viola!As long as the model for your template for your tags collection is of type
string
, MVC will iterate through the collection, so that it can be bound (though you may have to change fromICollection
to List).UPDATE
As we discussed in the comments, rather than creating one textbox per tag, create a separate ViewModel which has all of your other Product properties.
Instead of using
List<string> Tags
in your ViewModel, create this property:In your view, have a textbox for
TagCollection
. Then, in your Create action, you can parse the TagCollection string into your list of tags.