ASP MVC 3 RAZOR 动态表单生成帖子

发布于 2024-12-02 09:52:01 字数 1720 浏览 2 评论 0原文

我正在尝试使用带有文本字段的表单创建 asp mvc 3 页面,这些文本字段是根据我传递到页面控制器的模型中的列表中的对象数量动态生成的。我遇到的问题是,每当我发布到控制器时,列表都会维护值,但不会维护列表中对象的名称。这是一个示例:

模型:

public class SomeModel
    {
        List<Field> fieldList;
        public SomeeModel()
        {
            fieldList = new List<Field>();
        }


        public string Name { get; set; }

        public List<Field> FieldList
        {
            get
            {
                return fieldList;
            }
        }

    }

控制器:

public ActionResult Preview()
{
    SomeModel model = new SomeModel();
    model.FieldList.Add( new Field { name = "test 1"});
    model.FieldList.Add( new Field { name = "test 2"});
    model.FieldList.Add( new Field { name = "test 3"});

    return View(model);
}

视图:

@using (Html.BeginForm()) {
    <div>
        <fieldset>
            <legend>Input Field List</legend>

            @for (var i = 0; i < Model.FieldList.Count(); i++)
            {
                <div class="editor-label">
                   @Html.Label(Model.FieldList[i].name)
               </div>
               <div class="editor-field">
                   @Html.TextBoxFor(model => Model.FieldList[i].value)
               </div>
            }

             <p>
                <input type="submit" value="Generate PDF" />
            </p>
        </fieldset>
    </div>

我在这里看到了类似的帖子 .NET MVC Razor Dynamic Form Generation 但答案遇到了与我相同的问题。希望我说清楚了!如果没有,我会发布更多信息。谢谢!

I'm trying to create asp mvc 3 page with a form with text fields that are dynamically generated based on the number of object in a list that is in the model I passed into page controller. The problem I'm running into is that whenever I post to the controller the list maintains the values but not the names of the objects in the list. Here is an example:

The Model:

public class SomeModel
    {
        List<Field> fieldList;
        public SomeeModel()
        {
            fieldList = new List<Field>();
        }


        public string Name { get; set; }

        public List<Field> FieldList
        {
            get
            {
                return fieldList;
            }
        }

    }

The controller:

public ActionResult Preview()
{
    SomeModel model = new SomeModel();
    model.FieldList.Add( new Field { name = "test 1"});
    model.FieldList.Add( new Field { name = "test 2"});
    model.FieldList.Add( new Field { name = "test 3"});

    return View(model);
}

The View:

@using (Html.BeginForm()) {
    <div>
        <fieldset>
            <legend>Input Field List</legend>

            @for (var i = 0; i < Model.FieldList.Count(); i++)
            {
                <div class="editor-label">
                   @Html.Label(Model.FieldList[i].name)
               </div>
               <div class="editor-field">
                   @Html.TextBoxFor(model => Model.FieldList[i].value)
               </div>
            }

             <p>
                <input type="submit" value="Generate PDF" />
            </p>
        </fieldset>
    </div>

I saw a similar post here .NET MVC Razor Dynamic Form Generation but answer runs into the same issues as I got. Hope I'm being clear! If not I'll post more info. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

故事还在继续 2024-12-09 09:52:01

您可以将这些名称作为隐藏字段包含在内。这样它们的值将被发布到控制器操作:

@for (var i = 0; i < Model.FieldList.Count(); i++)
{    
    @Html.HiddenFor(model => Model.FieldList[i].name)
    ...
}

另外,我建议您使用编辑器模板而不是编写这些循环:

@model SomeModel
@using (Html.BeginForm()) 
{
    <div>
        <fieldset>
            <legend>Input Field List</legend>
            @Html.EditorFor(x => x.FieldList)
            <p>
                <input type="submit" value="Generate PDF" />
            </p>
        </fieldset>
    </div>
}

以及相应的编辑器模板(~/Views/Shared/EditorTemplates/Field.cshtml ),将为 FieldList 集合的每个元素呈现:

@model Field
@Html.HiddenFor(model => model.name)
<div class="editor-label">
    @Html.LabelFor(model => model.value, Model.name)
</div>
<div class="editor-field">
    @Html.TextBoxFor(model => model.value)
</div>

不使用隐藏字段的另一种可能性是将这些名称外部化到某个数据存储中,然后拥有一个可以返回它们的存储库。因此,在您的两个操作中,您只需查询此存储库中的名称即可。由于它们无法更改,因此无需将它们包含在 HTML 中。

You could include those names as hidden fields. This way their values will be posted to the controller action:

@for (var i = 0; i < Model.FieldList.Count(); i++)
{    
    @Html.HiddenFor(model => Model.FieldList[i].name)
    ...
}

Also instead of writing those loops I would recommend you using editor templates:

@model SomeModel
@using (Html.BeginForm()) 
{
    <div>
        <fieldset>
            <legend>Input Field List</legend>
            @Html.EditorFor(x => x.FieldList)
            <p>
                <input type="submit" value="Generate PDF" />
            </p>
        </fieldset>
    </div>
}

and the corresponding editor template (~/Views/Shared/EditorTemplates/Field.cshtml) which will be rendered for each element of the FieldList collection:

@model Field
@Html.HiddenFor(model => model.name)
<div class="editor-label">
    @Html.LabelFor(model => model.value, Model.name)
</div>
<div class="editor-field">
    @Html.TextBoxFor(model => model.value)
</div>

Another possibility instead of using hidden fields would be to externalize those names into some data store and then have a repository which would return them. So in your two actions you would simply query this repository for the names. Because they cannot be changed it is not necessary to include them in the HTML.

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