1

嗨,我必须用 ado.net 和 asp.net mvc 4 创建一个小项目。我现在必须将数据插入数据库,如果我能弄清楚如何访问发布的数据。

这是我的代码:

public ActionResult AddBook()
{
    return View(books);
}

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <label for="BookName">Book Name:</label><input type="text" name="BookName" />
    <label for="Author">Author:</label><input type="text" name="Author" />
    <label for="Description">Book Name:</label><input type="text" name="BookName" />
    <label>Choose category:</label>
    <select>
          @foreach (DataRow row in Model.Categories.Rows) { 
                <option value="@row["Id"]">@row["CategoryName"]</option>
          }
    </select>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

}

如何访问发布的数据?

4

1 回答 1

2

由于您尚未将视图绑定到Model类,因此您可以使用Request.Form[].

这就是你的做法:

public ActionResult AddBook(FormCollection collection)
{
    var bookname=collection.Get["BookName"];//here book name is the name of your input element
    ... similarily the rest
    ... do some database stuff
    return View();
}

或者

public ActionResult AddBook()
{
    var bookname=Request.Form["BookName"];//here book name is the name of your input element
    ... similarily the rest
    ... do some database stuff
    return View();
}
于 2013-02-26T12:50:10.493 回答