MVC .Net同时创建模型和相关模型 [英] MVC .Net create model and related models at same time

查看:54
本文介绍了MVC .Net同时创建模型和相关模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个称为Company的模型,并且company可以有1个或更多的董事.我希望能够在创建公司的同时创建任意数量的董事.

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.

我将Director的Create视图设为部分视图:

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)
 ...
}

在公司"的创建"视图上,我有一个按钮,可将Director的部分创建"视图加载到容器中:

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);
        });

这允许将Director的输入控件动态加载到公司"的创建"视图中.

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

我的问题是如何与公司同时处理董事的创建?还是只需要允许在编辑"上创建Director而不是创建"?

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?

推荐答案

几天前我开始学习mvc,今天我不得不做类似的事情.

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:

查看:

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

控制器:

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

这样,每个名为日期"的文本字段中的日期都将在列表日期"中显示.

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

这篇关于MVC .Net同时创建模型和相关模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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