0

您好我正在尝试在 asp.net MVC 中发布一个表单,但似乎当我单击提交按钮时我的控制器没有被调用。

我认为发生这种情况是因为每个输入的名称没有正确设置,因为我使用的是元组。我尝试使用 HTML 属性设置名称,但它似乎不起作用。

他是我的模特:

    public int Id { get; set; }
            public string Name { get; set; }
            public string Author { get; set; }
            public string Description { get; set;}
            public DateTime PublicationDate { get; set; }
            public int CategoryId { get; set; }

这是我的控制器:

    public ActionResult AddBook()
        {
            IEnumerable<CategoryModel> categories = categoryContext.GetCategories();
            Tuple<BookModel1, IEnumerable<CategoryModel>> model =
                new Tuple<BookModel1, IEnumerable<CategoryModel>>(book, categories);
            return View(model);
        }

        [HttpPost]
        public ActionResult AddBook(BookModel1 book)
        {
            bookContext.AddBook(book);
            return RedirectToAction("Index", "ProductManager");
        }

这是我的观点:

    @using (Html.BeginForm()) {
        @Html.ValidationSummary(true)
        <fieldset>
               @Html.LabelFor(x => x.Item1.Name , "Book Name")
               @Html.TextBoxFor(x => x.Item1.Name, new { name = "Name" })
               @Html.LabelFor(x => x.Item1.Author)
               @Html.TextBoxFor(x => x.Item1.Author , new { name = "Author" })
               @Html.LabelFor(x => x.Item1.PublicationDate ,"Publication Date")
               @Html.TextBoxFor(x => x.Item1.PublicationDate , new { name = "PublicationDate" })
               @Html.LabelFor(x => x.Item2 , "Select category")
               @Html.DropDownListFor(x => x.Item2, new SelectList(Model.Item2, "Id", "Name")) 
               @Html.LabelFor(x => x.Item1.Description)
               @Html.TextAreaFor(x => x.Item1.Description ,  new { name = "Description" })
            <p>
                <input type="submit" value="Create" class="link"/>
                @Html.ActionLink("Back to List", "Index", "ProductManager", null,new { @class ="link"})
            </p>
        </fieldset>
    }

即使我尝试使用 HTMLAttributes 设置名称属性,生成的名称仍然看起来像这样:Item1.PublicationDate。我正在使用 ADO.NET 进行数据访问。

我该如何解决这个问题,以便我可以调用AddBook(BookModel1 book)控制器?

4

2 回答 2

1

是的,问题是因为Tuple类。该Item属性是只读的,它没有设置器,这意味着默认模型绑定器无法在回发时设置其值。您应该改用视图模型:

public class MyViewModel
{
    public BookModel1 Book { get; set; }
    public IEnumerable<CategoryModel> Categories { get; set; }
}

然后使您的视图强类型化到视图模型:

@model MyViewModel
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        @Html.LabelFor(x => x.Book.Name, "Book Name")
        @Html.TextBoxFor(x => x.Book.Name, new { name = "Name" })
        @Html.LabelFor(x => x.Book.Author)
        @Html.TextBoxFor(x => x.Book.Author, new { name = "Author" })
        @Html.LabelFor(x => x.Book.PublicationDate, "Publication Date")
        @Html.TextBoxFor(x => x.Book.PublicationDate , new { name = "PublicationDate" })
        @Html.LabelFor(x => x.Book.CategoryId, "Select category")
        @Html.DropDownListFor(
            x => x.Book.CategoryId, 
            new SelectList(Model.Categories, "Id", "Name")
        ) 
        @Html.LabelFor(x => x.Book.Description)
        @Html.TextAreaFor(x => x.Book.Description, new { name = "Description" })
        <p>
            <input type="submit" value="Create" class="link"/>
            @Html.ActionLink("Back to List", "Index", "ProductManager", null, new { @class ="link"})
        </p>
    </fieldset>
}

最后是你的控制器:

public ActionResult AddBook()
{
    var model = new MyViewModel();
    model.Categories = categoryContext.GetCategories();
    model.Book = ...
    return View(model);
}

[HttpPost]
public ActionResult AddBook(MyViewModel model)
{
    bookContext.AddBook(model.Book);
    return RedirectToAction("Index", "ProductManager");
}
于 2013-02-28T10:26:56.917 回答
0

最好的方法不是使用元组,而是创建一个复杂的视图模型类,其中包含视图所需的所有数据。另一种方法是使用前缀绑定您的输入 BookModel1:

public ActionResult AddBook([Bind(Prefix = "Item1")]BookModel1 book) 
/*since you use Tuple and your bookmodel data is Item1*/

另外,检查您的表单是否正在发布以及您发布的数据(使用 fiddler/charles 或 firebug)

于 2013-02-28T10:26:42.147 回答