【问题标题】:How to bind dropdown value in mvc如何在mvc中绑定下拉值
【发布时间】:2017-06-19 13:16:39
【问题描述】:

我无法绑定下拉值。也许我在我的代码中遗漏了一些东西。在这里我附上我的视图代码。

<tr>
    <td>
        <div class="form-group" style="margin-bottom:0px;">                                                  
            @Html.DropDownList("ProofId", ViewBag.Idproofs as SelectList, new { @class = "form-control", id = "ProofType2", name = "ProofType2" })
        </div>
    </td>
    <td>
        <div class="form-group" style="margin-bottom:0px;">
            @Html.TextBoxFor(x => x.EvidenceData[1].ProofNumber, new { @class = "form-control", id = "IdNumber2", name = "IdNumber2" })
        </div>
    </td>
    <td>
        <div class="form-group" style="margin-bottom:0px;">
            @Html.TextBoxFor(x => x.EvidenceData[1].ProofImage, new { @class = "form-control", id = "ProofPic2", type = "file", name = "ProofPic2" })
            <a href="~/images/ProofPics/@Model.EvidenceData[1].ProofImage" download title="Download Image" style="text-decoration:underline;"><i class="fa fa-download" aria-hidden="true"></i> Download Image</a>
        </div>
    </td>
</tr>

这是我的控制器代码:

public ActionResult EditProfile(string country)
{
    if (!User.Identity.IsAuthenticated)
        return new RedirectResult(Utility.Config("RedirectPath"));
    var idprooflist = AccountManager.LoadProoftypes(country ?? "United States");
    ViewBag.Idproofs = new SelectList(idprooflist, "ProofId", "ProofName");
    UserRegistration UserProfileData = ProviderManagerBLL.GetProviderDetails(Convert.ToInt32(Session["UserID"].ToString()));
    return View(UserProfileData);
}

【问题讨论】:

  • 您的问题是什么?你不能在POST上找到它?还是什么?
  • 我无法绑定下拉值

标签: javascript c# asp.net-mvc


【解决方案1】:

以下内容应该适合您:

<div class="form-group" style="margin-bottom:0px;">                                                  
     @Html.DropDownList("ProofId", (SelectList)ViewBag.Idproofs, new { @class = "form-control", id = "ProofType2", name = "ProofType2" })
</div>

您正在使用ViewBag 将您的值传递给View,您所要做的就是参考您想要的内容。以前做的都行不通

【讨论】:

  • HtmlHelper 没有名为“DropDownList”的适用方法,但具有该名称的扩展方法。扩展方法不能动态调度
  • 我也试过这个但没有用 @Html.DropDownListFor(x => x.EvidenceData[1].ProofId, ViewBag.Idproofs as SelectList, "Select IdProof", new { @class= "表单控制”, id = "ProofType2", name = "ProofType2" })
  • 我已经更新了我的答案,我省略了我添加的一件事,(SelectList)ViewBag.Idproofs 而不仅仅是ViewBag.Idproofs
  • 您是否遇到同样的错误?那么为什么不通过您发送给您的Viewmodel 传递它呢?
  • 不是同一个错误。值尚未绑定,我尝试了@Html.DropDownListFor(x => x.EvidenceData[1].ProofId 之类的模型,它也不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多