MVC提交表单随着管窥 [英] MVC Submit Form With Partial View

查看:80
本文介绍了MVC提交表单随着管窥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

道歉,如果这已经回答过了,我无法找到我的问题匹配任何内容。

Apologies if this has been answered before, I can't find anything that matches my issue.

我有一个观点,它包含的局部视图,并提交查看时,从局部视图中的数据不会被发送到控制器。我明白,如果我使用一个编辑模板,这应该工作,但是我不能用这个,因为我从我的偏内侧主模型需要的数据。

I have a View, which contains a Partial View, and when submitting the View, the data from the partial view is not sent to the controller. I understand that if I use an editor template, this should work, but I'm unable to use this, as I need data from my main model inside the Partial.

我创建视图(简体):

@model CreateXmlSchemaModel
@using (Html.BeginForm("Create", "XmlSchema", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <fieldset>
        <div class="editor-label">
            @Html.LabelFor(m => m.XmlSchema.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(m => m.XmlSchema.Name)
        </div>
        <div id="fieldmapping">
            @Html.Partial("_XmlFieldMapping")
        </div>
        <div>
            <input type="submit" value="Create" class="button" />
        </div>
    </fieldset>
}

我的模型结构(简体):

My Model structure (simplified):

public abstract class XmlSchemaModelContainerBase
{
    public IEnumerable<string> BusinessObjects { get; set; }
    public IEnumerable<XmlInputFieldModel> XmlFields { get; set; }
    public IEnumerable<string> BusinessObjectFields { get; set; }
}

public class XmlSchemaModelContainer : XmlSchemaModelContainerBase
{
    public XmlSchemaModel XmlSchema { get; set; }
}

public class XmlSchemaModel
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string BusinessObject { get; set; }
    public IEnumerable<XmlSchemaField> MappedFields { get; set; }
}

public class XmlSchemaField
{
    public string XmlElement { get; set; }
    public string BusinessObjectField { get; set; }
}

public class XmlInputFieldModel
{
    public string XmlElement { get; set; }
    public string XmlValue { get; set; }
}

public class CreateXmlSchemaModel : XmlSchemaModelContainer
{
    [Required(ErrorMessage = "File upload required")]
    public HttpPostedFileBase Xml { get; set; }
}

我的控制器(我不会发布这一切,我可以立即告诉我的视图模型不包含MappedFields):

My Controller (I won't post it all, as I can tell immediately that my viewModel does not contains the MappedFields):

[HttpPost]
public ActionResult Create(CreateXmlSchemaModel viewModel)
{
}

我_XmlMappedFields局部视图:

My _XmlMappedFields Partial View:

@model CherwellXmlConnector.Models.XmlSchemaModelContainer
@{
    Model.XmlSchema.MappedFields = Model.XmlSchema.MappedFields.OrderBy(i => i.XmlElement);
}

<div>
    <div>
        @for(int i = 0; i < @Model.XmlFields.Count(); i ++)
        {
            List<CherwellXmlConnector.Models.XmlSchemaField> fields = Model.XmlSchema.MappedFields.ToList();
            string itemValue = Model.XmlFields.FirstOrDefault(x => x.XmlElement == fields[i].XmlElement).XmlValue;

            <div style="width: 300px; display: inline-block;">
                @Html.DisplayFor(x => fields[i].XmlElement)
            </div>
            <div style="width: 300px; display: inline-block;">
                @itemValue
            </div>
            <div class="busoblist" style="display: inline-block;">
                @Html.DropDownListFor(x => fields[i].BusinessObjectField, new SelectList(Model.BusinessObjectFields))
            </div>
            <hr />
        }
    </div>
</div>

我的目的是在部分显示给用户,所有给定XmlFields和它们的值的列表,并允许他们从每个XmlField一个下拉菜单中选择一个BusinessObjectField。那么我想这个提交回控制器的的IEnumerable&LT内部; XmlSchemaField&GT; MappedFields 对象。这可能吗?

推荐答案

是的,它是可能的。

您所面临的问题是由于如何模型绑定的工作。如果它只是一个基本类型您要绑定的的IEnumerable ,那么它会工作得很好。

The issue you are facing is due to how model binding work. If it was just a primitive type you are trying to bind for the IEnumerable, then it would have worked fine.

@foreach(var item in @Model.XmlSchema.MappedFields)
    {
        string itemValue = Model.XmlFields.FirstOrDefault(x => x.XmlElement == item.XmlElement).XmlValue;
        <div style="width: 300px; display: inline-block;">
            @Html.DisplayFor(i => item.XmlElement)
        </div>
        <div style="width: 300px; display: inline-block;">
            @itemValue
        </div>
        <div class="busoblist" style="display: inline-block;">
            @Html.DropDownListFor(i => item.BusinessObjectField, new SelectList(Model.BusinessObjectFields))
        </div>
        <hr />
    }

如果你看看上述code生成的源$ C ​​$ C,你会看到所有的的下拉列表中的选择标记)具有相同的名称。正因为如此,它不会绑定到你的模型正确时,您提交表单。

if you take a look at the source code generated by the above code, you will see all of the dropdowns (select tags) generated from the above code has the same name. because of this, it will not bind to your model properly when you submit the form.

下面是一个很好的博客文章,我发现非常有用的,它详细介绍了这个

Following is a good blog post which I found very useful and it explains about this in detail

http://haacked.com /archive/2008/10/23/model-binding-to-a-list.aspx/

更新

对于那些你们谁到这里来寻找张贴数据列表的答案,我写了一个博客文章。
http://amilaudayanga.word$p$pss.com/2014/03/05/posting-data-to-a-list-in-asp-net-mvc-part1/

For those of you who come here looking for an answer for posting data to a list, I've written a blog post. http://amilaudayanga.wordpress.com/2014/03/05/posting-data-to-a-list-in-asp-net-mvc-part1/

这篇关于MVC提交表单随着管窥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆