【问题标题】:In MVC3 how to save more models at time from a View?在 MVC3 中,如何一次从视图中保存更多模型?
【发布时间】:2012-11-10 03:19:43
【问题描述】:

在我的 MVC3 项目中,我有一个这样的模型

Public Class Description

    Public Property DescriptionId As Integer

    <DisplayName("Product Title"), Required(ErrorMessage:="A Product Title is required"), StringLength(160)>
    Public Property ProductTitle As String

    <DisplayName("Product Description"), DataType(DataType.MultilineText)>
    Public Property ProductDescription As String

End Class

如下图所示

@ModelType MyProject.Description

@Code
    ViewData("Title") = "Create"
End Code

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@Using Html.BeginForm()
    @Html.ValidationSummary(True)
    @<fieldset>
        <legend>Description</legend>

        <div class="editor-label">
            @Html.LabelFor(Function(model) model.ProductTitle)
        </div>
        <div class="editor-field">
            @Html.EditorFor(Function(model) model.ProductTitle)
            @Html.ValidationMessageFor(Function(model) model.ProductTitle)
        </div>

        <div class="editor-label">
            @Html.LabelFor(Function(model) model.ProductDescription)
        </div>
        <div class="editor-field">
            @Html.EditorFor(Function(model) model.ProductDescription)
            @Html.ValidationMessageFor(Function(model) model.ProductDescription)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
End Using

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

我可以一次创建一个新的描述,但是有没有一种方法可以一次插入更多的描述(例如五个描述)以及如何在控制器动作中获取它们?

【问题讨论】:

  • 使用网格,就像任何JQuery Grid 一样,而不是带有字段的表单(JQuery UI Grid 是一个非常好的表单),并使用 List 作为模型

标签: asp.net-mvc asp.net-mvc-3 razor


【解决方案1】:

不太清楚VB.net的语法,我提供了它的c#版本。

  1. 在您的视图模型中使用集合,例如List&lt;String&gt; ProductDescription {get;set;}

  2. 在视图中,手动渲染描述,避免EditorFor

    for (var i = 0; i < Model.ProductDescription.Count;i++ ) { @Html.TextBox("ProductDescription[" + i + "]") }

  3. 回发时,使用描述作为您的操作参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 2012-06-27
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多