我应该如何使用这个简单的为 ASP.NET MVC3 视图创建视图来编程该控制器 ActionResult 方法?

发布于 2024-12-09 04:31:52 字数 2119 浏览 3 评论 0原文

我不确定是否应该使用 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 技术交流群。

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

发布评论

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

评论(1

乖乖兔^ω^ 2024-12-16 04:31:52

由于您有一个强类型视图,因此您可以让您的 [HttpPost] 方法采用与变量相同的类型,例如:

[HttpPost]
public ActionResult Create(Product model)  
{  
    if (ModelState.IsValid)
    {
        // add new product
        .... ????
    } 
}  

DefaultModelBinder 将采用返回的值,并插入将它们放入您的强类型模型中。中提琴!

只要标签集合模板的模型是 string 类型,MVC 就会迭代该集合,以便可以绑定它(尽管您可能必须从 ICollection< /code> 到列表)。

更新

正如我们在评论中讨论的,不要为每个标签创建一个文本框,而是创建一个单独的 ViewModel,其中包含所有其他产品属性。

而不是使用 List;在 ViewModel 中的 Tags 中,创建此属性:

public string TagCollection { get; set; }

在您的视图中,有一个 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.:

[HttpPost]
public ActionResult Create(Product model)  
{  
    if (ModelState.IsValid)
    {
        // add new product
        .... ????
    } 
}  

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 from ICollection 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:

public string TagCollection { get; set; }

In your view, have a textbox for TagCollection. Then, in your Create action, you can parse the TagCollection string into your list of tags.

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