DropDownList不选择视图模型字段

时间:2016-08-28 16:52:37

标签: c# .net asp.net-mvc dropdownlistfor

不确定我做错了什么,但我的下拉列表并不想选择我想要它选择的值。我有以下

控制器操作

// GET: /Contract/Create
public ActionResult Create()
{
    var model = new ContractViewModel();

    var authors = _authorService.GetAuthors();
    var publishers = _publisherService.GetPublishers();

    model.AuthorsList = new SelectList(authors, "AuthorID", "Name", authors.First());
    model.PublishersList = new SelectList(publishers, "PublisherID", "Name", publishers.First());

    return View(model);
}

// POST: /Contract/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ContractViewModel contractViewModel)
{
    if (ModelState.IsValid)
    {
        Contract contract = new Contract();
        contract.CanUsePublisherPartners = contractViewModel.CanUsePublisherPartners;

        contract.Author.AuthorID = Convert.ToInt32(contractViewModel.SelectedAuthorID);
        contract.Publisher.PublisherID = Convert.ToInt32(contractViewModel.SelectedPublisherID);


        var success = _contractService.AddContract(contract);
        if (success)
        {
            return RedirectToAction("Index");
        }
    }

    contractViewModel.AuthorsList = new SelectList(_authorService.GetAuthors(), "AuthorID", "Name");
    contractViewModel.PublishersList = new SelectList(_publisherService.GetPublishers(), "PublisherID", "Name");

    ViewBag.ErrorMessage = "An error occured when trying to add the Contract. A contract between this Author and Publisher may already exist! Please try again and if the problem persists, contact the Sys Admin.";
    return View(contractViewModel);
}

视图模型

public class ContractViewModel
{
    [Display(Name = "Can the author distribute through the publisher's partners?")]
    public bool CanUsePublisherPartners { get; set; }
    [Display(Name="Author")]
    public int? SelectedAuthorID { get; set; }
    [Display(Name = "Publisher")]
    public int? SelectedPublisherID { get; set; }

    public SelectList AuthorsList { get; set; }
    public SelectList PublishersList { get; set; }

}

查看下拉列表的绑定

<div class="form-group">
    @Html.LabelFor(model => model.SelectedAuthorID, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.HiddenFor(m => m.SelectedAuthorID)
        @Html.DropDownListFor(m => m.SelectedAuthorID, Model.AuthorsList)
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(model => model.SelectedPublisherID, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.HiddenFor(m => m.SelectedPublisherID)
        @Html.DropDownListFor(m => m.SelectedPublisherID, Model.PublishersList)
    </div>
</div>

问题是什么? 当我提交表单时, SelectedAuthorID SelectedPublisherID 的值是默认值 int - 0。

我真的在这里结识,我看了一些细节,试图找出它们是否会影响任何事情。例如。当Selected容器属性与列表项的value属性具有相同的名称时,有些人遇到了麻烦,等等。

如果有人有任何建议会很高兴分享!

1 个答案:

答案 0 :(得分:0)

我认为问题在于您在网页上有SelectedPublisherIDSelectedAuthorID两次。

Html.HiddenFor(m => m.SelectedAuthorID)旁边不需要DropDownListFor

一个小问题,一般的C#命名约定使用PascalCase,这意味着属性应该命名为SelectedAuthorId而不是ID