没有选择视图模型字段的值

本文关键字:字段 模型 有选择 视图 | 更新日期: 2023-09-27 18:04:19

不知道我做错了什么,但我的下拉菜单不想选择我想要选择的值。我有以下

<<p> 控制器操作/strong>
// 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);
}

ViewModel

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>

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

我真的束手无策,我看了一些细节,试图找出它们是否会影响什么。例如,当Selected container属性与列表项的value属性名称相同时,有些人会遇到麻烦,等等。

如果有人有任何建议,将是伟大的分享他们!

没有选择视图模型字段的值

我认为问题是你有SelectedPublisherIDSelectedAuthorID在页面上两次。

Html.HiddenFor(m => m.SelectedAuthorID)不应该和DropDownListFor放在一起。

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