簡體   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

我正在嘗試將此表格傳遞給我的控制器。 我在主模型中有另一個模型。

他的模特和形式是這樣的。 不知道控制器是否會像這樣拾取模型

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

對於主管的詳細信息,您必須相應地將其命名為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM