简体   繁体   English

C#-如何在ASP.NET MVC的HTML表单中传递内部带有其他模型的模型

[英]C# - How do I pass a model with another model inside from a HTML form in ASP.NET MVC

I have this form I'm trying to pass to my controller. 我正在尝试将此表格传递给我的控制器。 I have another model inside the main model. 我在主模型中有另一个模型。

he models and the form are like this. 他的模特和形式是这样的。 Don't know if the controller will pickup the model like this 不知道控制器是否会像这样拾取模型

public class Entry{
    public string id { get; set;}
    public string department { get; set; }
    public Supervisor supervisor { get; set; }
}
public class Supervisor{
    public string name { get; set; }
    public string employeeID { get; set; }
}
<form action="Home/Upload" method="post" style="border:solid #000 1px; padding:5px">
    <fieldset>
        <legend>Entry</legend>

        ID: <input type="text" name="id" />
        Department: <input type="text" name="department" />
        <br /><br />

        Supervisor<br />
        Name: <input type="text" name="name"/>
        Employee Id: <input type="text" name="employeeID"/><br />

        <input type="submit" value="Submit"/>
    </fieldset>
</form>
public class HomeController : Controller {
    [HttpPost]
    public ActionResult Upload(Entry newEntry){
        Database db = new Database();
        bool result = db.add(newEntry);
        return View("UploadResult", result);
    }
}

For the supervisor details you have to name it accordingly as : 对于主管的详细信息,您必须相应地将其命名为:

    Name: <input type="text" name="supervisor.name"/>
    Employee Id: <input type="text" name="supervisor.employeeID"/><br />

暂无
暂无

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

相关问题 如何将模型值作为javascript函数参数从asp.net mvc Html助手中传递出来,如Html.Radio? - How do I pass model values as javascript function parameters from inside an asp.net mvc Html helper such a Html.Radio? 如何使用 C# 在 ASP.NET 核心 MVC 中将 model 从一个动作传递到另一个动作而不使用 TempData - How to pass model from one action to another without using TempData in ASP.NET Core MVC using C# 如何在 Dropzone.js ASP.NET MVC C# 中传递 model 时传递图像 - How do I pass the images while passing a model in Dropzone.js ASP.NET MVC C# ASP.NET MVC:如何将列表(从Model中的类)传递到View中的转发器? - ASP.NET MVC: How do I pass a list (from a class in Model) to a repeater in a View? Asp.net MVC C#一种形式,两种模型 - Asp.net MVC C# One Form, Two Model C# 和 ASP.NET MVC:如何在另一个模型中填充数据对象模型 - C# & ASP.NET MVC: How to populate a data object model within another model 如何在ASP.NET MVC 4中显示从模型到另一个视图的列表 - How do i show a list from a model to another View in ASP.NET MVC 4 如何在ASP.NET MVC的父域模型中将此子模型引用传递给虚拟ICollection? - How do I pass this child model reference to a virtual ICollection in parent domain model in ASP.NET MVC? 从ASP.NET MVC中的表单发布到另一个模型 - Posting to another model from a form in ASP.NET MVC 如何在 asp.net MVC 中使用 RedirectToAction 将模型从一种 Action 方法传递到另一种方法? 尽管我发送了一个填充模型,但我得到了空模型 - How to pass model from one Action method to another using RedirectToAction in asp.net MVC ? I am getting null model though I send a populated model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM