MVC .Net create model and related models at same time

早过忘川 提交于 2019-12-31 07:26:07

问题


I have a model called Company and company can have 1 or more directors. I want to be able to create any number of directors at the same time as creating a company.

I made the Create view of the of the Director a partial view:

@using (Ajax.BeginForm("Create", 
                       "Director", 
                       new AjaxOptions { 
                         HttpMethod = "POST", 
                         UpdateTargetId = "partial", 
                         InsertionMode = InsertionMode.InsertAfter }))
{
 @Html.EditorFor(model => model.Title)
 ...
}

On the Create view of the Company I have a button which loads the partial Create view of the Director into a container:

$.get('@(Url.Action("Create", "Director"))', function (result) {  
            $('#partial').append(result);
        });

This allows the input controls of the Director to be dynamically loaded into the Create view of the Company.

My question is how to handle the creation of Directors at the same time as the company? Or do I need to only allow creation of Directors on Edit and not Create?


回答1:


I started learning mvc few days ago and today I had to do something similar.

I wanted to save multiple dates at the same time. Here is the simplified code:

View:

<input type="text" name="Date" />

Controller:

public ActionResult Create(List<DateTime> Date)
{
    //Saving goes here
}

This way date from every text field named "Date" will be available in List Date.



来源:https://stackoverflow.com/questions/10327916/mvc-net-create-model-and-related-models-at-same-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!