MVC 5多选列表值未绑定到帖子上的模型

时间:2016-05-25 19:36:44

标签: model-binding asp.net-mvc-5 html.listboxfor

我有一个带有几个多选列表的视图,这些列表被声明为

.animate()

我的视图模型包含如下声明:

<div class="col-md-6">
    @Html.LabelFor(model => model.Counties, htmlAttributes: new { @class = "control-label" })
    @Html.ListBoxFor(model => model.Counties, new MultiSelectList(ViewBag.CountyList, "Value", "Text"), htmlAttributes: new { @class = "form-control", size = 8, tabindex = 26 })
    @Html.ValidationMessageFor(model => model.Counties, "", new { @class = "text-danger" })
    <span class="small">Ctrl + click to select multiple items</span>
</div>

我的行动如下:

 public virtual List<long> Counties { get; protected set; }

在提交时,我可以查看浏览器发送的帖子数据,并将相应的数据发送到服务器

    [HttpPost]
    public ActionResult Edit(TScholarshipView model, FormCollection form)
    {
        if (ModelState.IsValid)
        {
            TScholarship scholarship = Repo.GetScholarship(model.Id);
            scholarship = Mapper.Map<TScholarshipView, TScholarship>(model, scholarship);
            Repo.SaveOrUpdate(scholarship, HttpContext.User.Identity.Name);

            return RedirectToAction("Edit", "AdminScholarship", new { id = model.Id });
        }

        return View("Scholarship", model);
    }

当动作开始执行时,模型中的Counties值为null。但是我可以查看FormCollection值,表单[“Counties”]的值是“16,34”。任何想法绑定没有发生的想法?

2 个答案:

答案 0 :(得分:1)

我在发布问题后立即注意到了这一点。问题是让二传手受到保护。这阻止了活页夹设置列表的值。

答案 1 :(得分:0)

您还需要在post事件上重置ViewBag.CountyList的值。

或者在模型中有一个属性并将该属性绑定到多选列表框。 像

这样的东西

Wrapper / Model

$db = new PDO("mysql:host=$hostname;dbname=centers", $dbusername, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $db->prepare("INSERT INTO cent_log (ip, message, time, username) VALUES (:val1, :val2, :val3, :val4)");
$stmt->bindParam(':val1', $_SERVER['REMOTE_ADDR']);
$stmt->bindParam(':val2', $e);
$stmt->bindParam(':val3', $_SERVER['REQUEST_TIME']);
$stmt->bindParam(':val4', $_SESSION['user_IP']);
$stmt->execute();

控制器

<xsl:variable name="Date" select="//MEDIAITEMS_ROW/CREATED_DATE"/>
<xsl:variable name="Day" select="substring($Date,1,2)" />
<xsl:variable name="month" select="substring($Date,4,3)" />
<xsl:variable name="Year" select="substring($Date,8,2)" />
<xsl:template name="get-month-abbreviation">
        <xsl:choose>
            <xsl:when test="$month = JAN">01</xsl:when>
            <xsl:when test="$month = FEB">02</xsl:when>
            <xsl:when test="$month = MAR">03</xsl:when>
            <xsl:when test="$month = APR">04</xsl:when>
            <xsl:when test="$month = MAY">05</xsl:when>
            <xsl:when test="$month = JUN">06</xsl:when>
            <xsl:when test="$month = JUL">07</xsl:when>
            <xsl:when test="$month = AUG">08</xsl:when>
            <xsl:when test="$month = SEP">09</xsl:when>
            <xsl:when test="$month = OCT">10</xsl:when>
            <xsl:when test="$month = NOV">11</xsl:when>
            <xsl:when test="$month = DEC">12</xsl:when>
            <xsl:otherwise>error: <xsl:value-of select="$month"/></xsl:otherwise>
        </xsl:choose>

</xsl:template>

查看

public class CustomerList
{
    public List<Customer> Customers { get; set; }
    public List<int> SelectedIDs { get; set; }
}

如上所述here

希望有效!!!