【问题标题】:Bind Textbox to model in MVC4将文本框绑定到 MVC4 中的模型
【发布时间】:2013-05-03 11:35:12
【问题描述】:

在我的 MVC 应用程序中,我根据 Model.Count 指定了两个条件以在 View 中显示值。
查看

  @model IEnumerable<SampleECommerce.Models.DetailsModel>
  @using (Html.BeginForm("Details", "Grid", new { UserID = Request.QueryString["UserID"], partnerid = Request.QueryString["Partnerid"] }, FormMethod.Post))
    {
    if (Model.Count() == 0)
    {
    @foreach (var item in Model)
    {
        <table>
            <tr>
                <td>
                    @Html.DisplayNameFor(model => model.FirstName)
                    <input id="FirstName" type="text" class="TextBoxBorder" name="FirstName" value="@item.FirstName" /> // When the Model count is zero, the label and textbox is not displayed.
                </td>
            </tr>
        </table>
    }
    else
    {
    @foreach (var item in Model)
    {
        <table>
            <tr>
                <td>
                    @Html.DisplayNameFor(model => model.FirstName)
                    <input id="MFirstName" type="text" class="TextBoxBorder" name="FirstName" value="@item.FirstName" />
                </td>
            </tr>
        </table>
    }


控制器

    public ActionResult Details()
       {
           string userid = Request.QueryString["UserID"];
           string partnerid = Request.QueryString["Partnerid"];

           con.Open();
           SqlCommand cmd = new SqlCommand("select FirstName from Details where UserID = +userid+", con);

       SqlDataReader dr = cmd.ExecuteReader();
       List<DetailsModel> objmodel = new List<DetailsModel>();
       while (dr.Read())
       {
           objmodel.Add(new DetailsModel()
           {
               FirstName = dr["First Name"].ToString(),


           });
       }
       dr.Close();

       return View(objmodel);
   }

当 Model.Count 为零时,不显示标签和文本框。
当model.count基于用户ID为零时,我正在尝试向文本框插入新值
我尝试将文本框绑定到对Link 中指定的所有方式进行建模。
1.@Html.TextBoxFor(model => model.FirstName) FirstName 出现错误,说明“System.Collections.Generic.IEnumerable 未找到 FirstName 的定义或没有扩展方法”
2.@Html.TextBox(model=>model.FirstName) “错误说明无法将 Lamba 表达式转换为字符串类型”
当 model.count 为零时如何将文本框值绑定并显示到模型。 有什么建议吗??

【问题讨论】:

    标签: c# asp.net-mvc-3 textbox model-binding


    【解决方案1】:

    Model.Count 为0 时,foreach 什么都不做。

      @model IEnumerable<SampleECommerce.Models.DetailsModel>
      @using (Html.BeginForm("Details", "Grid", new { UserID = Request.QueryString["UserID"], partnerid = Request.QueryString["Partnerid"] }, FormMethod.Post))
        {
            <table>
            if (Model.Count() == 0)
            {
    
                    <tr>
                        <td>
                            @Html.DisplayNameFor(model => model.FirstName)
                            <input id="FirstName" type="text" class="TextBoxBorder" name="FirstName" /> // When the Model count is zero, the label and textbox is not displayed.
                        </td>
                    </tr>
            }
            else
            {
                    @foreach (var item in Model)
                    {
    
                            <tr>
                                <td>
                                    @Html.DisplayNameFor(model => model.FirstName)
                                    <input id="MFirstName" type="text" class="TextBoxBorder" name="FirstName" value="@item.FirstName" />
                                </td>
                            </tr>
    
                    }
            }
            <tr>
                <td>
                    <input type="submit" value="submit" />
                </td>
            </tr>
            </table>
        }
    

    【讨论】:

    • 当 Model.Count 为零时,我可以将文本框绑定到模型值吗??
    • 当文本框为空时,我需要根据用户标识从文本框中插入新值
    • 当页面加载时,首先我会显示一个空的文本框。如果模型值基于用户 ID 存在,则该值显示在文本框中。否则从文本框中插入新值并更新它。
    • form标签中new { UserID = Request.QueryString["UserID"], partnerid = Request.QueryString["Partnerid"] }这行是什么意思?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多