2013-05-03 52 views
0

在我的MVC应用程序中,我已经根据Model.Count指定了两个条件以在View中显示值。
查看将文本框绑定到MVC4中的模型

@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是零,不显示在标签和文本框。
我试图插入新的价值的文本框时,基于用户标识
我试图将文本框在Link规定的所有方法建模结合model.count为零。
1. @ Html.TextBoxFor(型号=> model.FirstName) 错误姓陈述 “System.Collections.Generic.IEnumerable不列入找到姓定义或无扩展方法”
2. @ Html.TextBox (model => model.FirstName) “错误说明无法将Lamba表达式转换为字符串类型”
如何在model.count为零时将文本框值绑定并显示到模型。 有什么建议?

回答

2

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> 
    } 
+0

莫非我能绑定文本框模型值,当Model.Count为零? – kk1076 2013-05-03 11:43:58

+0

当文本框为空时,我需要基于用户标识符从文本框中插入新值 – kk1076 2013-05-03 11:52:03

+0

当页面加载时,首先我显示一个空的文本框。如果基于用户标识存在模型值,则该值将显示在文本框中。否则从文本框中插入新值并更新它。 – kk1076 2013-05-03 12:36:41