【问题标题】:C# - How do I pass a model with another model inside from a HTML form in ASP.NET MVCC# - 如何从 ASP.NET MVC 中的 HTML 表单中传递带有另一个模型的模型
【发布时间】:2019-07-09 05:15:38
【问题描述】:

我正在尝试将这个表单传递给我的控制器。我在主模型中有另一个模型。

他的模型和形式是这样的。不知道控制器会不会像这样拾取模型

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

【问题讨论】:

标签: c# html asp.net model-view-controller


【解决方案1】:

对于主管的详细信息,您必须将其命名为:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    • 2018-12-02
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    相关资源
    最近更新 更多