簡體   English   中英

列表框未發布到MVC控制器

[英]ListBox not posting to MVC controller

我有一個帶有一些文本Area和ListBox的表單。 表單中的所有其他字段都將發布到控制器,但不會發布到ListBox嗎?

這是剃刀代碼:

 <form method="post" action="/Admin/AddPost">
    @Html.ValidationSummary(true)
    <div class="form-group">
        <label>Title</label>
        <input type="text" class="form-control" name="BlogTitle" placeholder="Title" value="@Model.BlogTitle" required>
    </div>
    <div class="form-group">
        <label>Image</label>
        <input type="text" class="form-control" name="BlogImage" value="@Model.BlogImage" placeholder="Image">
    </div>
    <div class="form-group">
        <label>Description</label>
        <textarea name="BlogDescription">@Model.BlogDescription</textarea>
    </div>
    <div class="form-group">
        <label>Tags</label>
        @Html.ListBox("Tags", Model.Tags, new { @class = "form-control", @name = "Tags", @multiple = "multiple" })
    </div>
    <button type="submit" class="btn btn-primary">Save</button>
</form>

這是控制器的GET和POST:

 public ActionResult AddPost()
    {
        var model = new BlogModel
        {
            BlogTitle = string.Empty,
            BlogDescription = string.Empty,
            BlogImage = string.Empty,
            Tags = new TagQueries().GetAllTags().Select(x => new SelectListItem
            {
                Text = x.TagName,
                Value = x.TagId.ToString(),
                Selected = false
            })
        };
        return View(model);
    }
    [HttpPost]
    public ActionResult AddPost(BlogModel model)
    {
        TransferBlogDetailsToDbModel addToDb = new TransferBlogDetailsToDbModel();
        if (ModelState.IsValid && model != null)
        {
            try
            {
                addToDb.TransferBlogDetails(model);
                return RedirectToAction("Index", "Admin");
            }
            catch (Exception)
            {
                ModelState.AddModelError("", "Invalid data,Please try again");
            }
        }
        return View(model);
    }

這是模型:

public class BlogModel
{
    [HiddenInput]
    public int BlogId { get; set; }

    [Required(ErrorMessage = "Blog Title is required")]
    public string BlogTitle { get; set; }
    [Required(ErrorMessage = "Blog description is required")]
    public string BlogDescription { get; set; }
    public string BlogImage { get; set; }

    public IEnumerable<SelectListItem> Tags { get; set; }
}

任何建議表示贊賞。

您的屬性TagsIEnumerable<SelectListItem>並且不能將<select multiple>綁定到復雜對象的集合。 <select>返回一個簡單值(所選選項的值)數組。

您的模型需要綁定一個屬性,例如

public IEnumerable<int> SelectedTags { get; set; }

假設TagTagId是typeof int 然后視圖中的代碼是

 @Html.ListBoxFor(m => m.SelectedTags , Model.Tags, new { @class = "form-control" })

旁注:

  1. 不要使用new { @name = "..." } -幸運的是它什么都不做,並且永遠不要嘗試覆蓋HtmlHelper方法生成的name屬性。
  2. 不要使用new { @multiple = "multiple" } - ListBox()ListBoxFor()方法已經生成了
  3. 使用TextBoxFor() and TextAreaFor()方法生成其他表單控件,以便獲得正確的雙向模型綁定
  4. 刪除SelectListItem構造函數中的Selected = false代碼(不僅默認情況下為false,而且無論如何都會忽略該值-確定SelectedTags內容的SelectedTags的值)

僅使用IEnumerable填充框,並添加另一個屬性以保存ListBox中選定項值的列表,如下所示:

public IEnumerable<string> SelectedTags { get; set; }

注意:SelectedTags填充將取決於創建SelectListItem的方式。 如果設置了SelectListItem.Value,則SelectedTags將包含這些值,否則,SelectedTags將使用SelectedListItem.Text屬性的值填充。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM