2013-05-03 2 views
0

MVC 애플리케이션에서 View의 값을 표시하기 위해 Model.Count를 기반으로 두 조건을 지정했습니다.
보기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이 0 레이블과 텍스트 상자가 표시되지 않습니다.
model.count가 사용자 ID를 기반으로 0 일 때 텍스트 상자에 새 값을 삽입하려고합니다.
Link에 지정된 모든 방법을 모델링하기 위해 텍스트 상자를 바인딩하려고했습니다.
1. @ Html.TextBoxFor (모델 => model.FirstName)
2 @ "System.Collections.Generic.IEnumerable은 FIRSTNAME에 대한 정의 또는 전혀 확장 방법을 찾을 수 doesnot"라는 FIRSTNAME에서 오류 Html.TextBox (model => model.FirstName) "Lamba 표현식을 문자열 유형으로 변환 할 수 없습니다"
model.count가 0 일 때 텍스트 상자 값을 모델에 바인딩하고 표시하는 방법. 제안 사항 ??

답변

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가 0 일 때 텍스트 상자를 모델 값에 바인딩 할 수 있습니까? – kk1076

+0

텍스트 상자가 비어있을 때 사용자 ID를 기반으로 텍스트 상자에서 새 값을 삽입해야합니다. – kk1076

+0

페이지가로드되면 먼저 비어있는 텍스트 상자를 표시합니다. 모델 값이 사용자 ID를 기반으로 표시되면 값이 텍스트 상자에 표시됩니다. 그렇지 않으면 텍스트 상자에서 새 값을 삽입하고 업데이트하십시오. – kk1076

관련 문제