3

我知道如何post在 ASP.NET 中将对象列表添加到表单中,但假设我想同时获得post一些其他值?

有没有办法有一个表格,像这样

<form method="POST" action="test">
    <input type="text" name="name" value="Roryok" />
    <input type="text" name="email" value="idontgiveoutmyemail@overtheinter.net" />
    <input type="text" name="[0].hobby" value="dibbling" />
    <input type="text" name="[0].level" value="amateur" />
    <input type="text" name="[0].hobby" value="fargling" />
    <input type="text" name="[0].level" value="intermediate" />
    <input type="text" name="[2].hobby" value="garbling" />
    <input type="text" name="[2].level" value="expert" />
</form>

发布到一个看起来像这样的方法?

[HttpPost]
public ActionResult test(MyViewModel model){
    /// do some stuff with the model here 
    return View();
}

public class MyViewModel{
    public string name { get; set; }
    public string email { get; set; }
    public List<Hobby> hobbies { get; set; }
}

public class Hobby{
    public string hobby { get; set; }
    public string level { get; set; }
}
4

1 回答 1

5

尝试这个:

<form method="POST" action="test">
    <input type="text" name="name" value="Roryok" />
    <input type="text" name="email" value="idontgiveoutmyemail@overtheinter.net" />
    <input type="text" name="hobbies[0].hobby" value="dibbling" />
    <input type="text" name="hobbies[0].level" value="amateur" />
    <input type="text" name="hobbies[1].hobby" value="fargling" />
    <input type="text" name="hobbies[1].level" value="intermediate" />
    <input type="text" name="hobbies[2].hobby" value="garbling" />
    <input type="text" name="hobbies[2].level" value="expert" />
</form>
于 2013-07-04T10:47:30.897 回答