简体   繁体   中英

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 />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM