简体   繁体   English

ASP.NET MVC3。模型包含对象的list <>,将其传递给视图,但返回为NULL

[英]ASP.NET MVC 3. Model contains list<> of objects, pass it to the view, but it returns as NULL back

I'm working with ASP.NET MVC 3 (APSPxView Engine). 我正在使用ASP.NET MVC 3(APSPxView Engine)。

I have a model (MasterModel) which contains some properties. 我有一个包含一些属性的模型(MasterModel)。 One of them is a list of objects (DetailModels): 其中之一是对象列表(DetailModels):

public class MasterModel
{
    [DisplayName("Master Name")]
    public string MasterName { get; set; }

    [DisplayName("Master Value")]
    public int? MasterValue { get; set; }

    public List<DetailModel> Details { get; set; }
}

public class DetailModel
{
    [DisplayName("Detail Name")]
    public string DetailName { get; set; }

    [DisplayName("Detail Value")]
    public int? DetailValue { get; set; }
}

So I need to make a view with form on it where I can fill MasterValue property and I want to make a GridView for the Details field and fill DetailValues inside the gridview. 因此,我需要在其上形成一个带有窗体的视图,在其中可以填充MasterValue属性,并且要为Details字段创建一个GridView并在gridview内填充DetailValues。

So my controller methods looks so: 所以我的控制器方法看起来像这样:

public ActionResult MasterDetailsView() 
{
    List<DetailModel> details = new List<MasterDetail>
    {
        new DetailModel
        {
            DetailName = "detail0",
        },
        new DetailModel
        {
            DetailName = "detail1",
        },
        new DetailModel
        {
            DetailName = "detail2",
        },
    };

    MasterModel master = new MasterModel
    {
       MasterName = "Master",
       Details = details
    };

    return View("MasterDetailsView", master);
}

[HttpPost]
public ActionResult MyMasterDetailsViewResponseHandler ([ModelBinder(typeof(DevExpressEditorsBinder))] MasterModel model)
    {
        // Some code
    }

And finally my view: 最后是我的观点:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Main.Master" Inherits="System.Web.Mvc.ViewPage<MyApp.Models.MasterModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<%      
using (Html.BeginForm("MyMasterDetailsViewResponseHandler", "Home", FormMethod.Post))
{
%>

<%= Html.LabelFor(m => m.MasterName) %>
<%= Html.TextBoxFor(m => m.MasterName)%>
<%= Html.LabelFor(m => m.MasterValue) %>
<%= Html.TextBoxFor(m => m.MasterValue)%>

//Some code to add Grid view for Details

Html.DevExpress().Button(settings =>
{
    settings.Name = "btnSubmit";
    settings.Width = Unit.Percentage(100);
    settings.Text = "Submit";     
    settings.UseSubmitBehavior = true;
}).GetHtml();  

<%      
}
%>
</asp:Content>

So, my first and main question: If I submit my form it returns me a model where MasterValue is filled properly but Details property is null. 因此,我的第一个也是主要的问题:如果我提交表单,它将返回一个模型,其中MasterValue正确填充,但是Details属性为null。 Even if I didn't add GridView just pass Details to the view and submit form back without any Details changes. 即使我没有添加GridView,也只需将Details传递给视图,然后将表单提交回去,而无需更改Details。 Details is null anyway. 无论如何,细节为空。 Could you help me to fix my problem, please? 您能帮我解决我的问题吗?

Second question is how to make GridView correctly for the Details where I can fill it and submit everything by one submit button click? 第二个问题是如何使GridView正确地显示在Details上,我可以在其中填充并单击提交按钮来提交所有内容?

the first issue I see is you are trying to webforms controls within the MVC framework. 我看到的第一个问题是您正在尝试在MVC框架内使用Webforms控件。 this doesn't work. 这行不通。 (or if it does work, it should be avoided). (或者如果确实有效,则应避免使用)。 webforms is a very unique html engine that is tightly integrated within itself. webforms是一个非常独特的html引擎,它本身紧密集成。

The "webforms" view engine for MVC is not the same as the Webfroms html engine with server side controls, viewstate, postback, etc. 用于MVC的“ webforms”视图引擎与具有服务器端控件,viewstate,postback等的Webfroms html引擎不同。

So there is no GridView or DetailsView controls. 因此,没有GridView或DetailsView控件。 You build out the html templates by hand. 您可以手动构建html模板。 There isn't any viewstate so anything you want to submit to the server must be rendered on the form. 没有任何视图状态,因此任何要提交到服务器的内容都必须在表单上呈现。 (typically ids as hidden fields, etc.) (通常是隐藏字段等ID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM