2014-11-03 179 views
0

行,所以我有以下情形ASP.Net MVC創建視圖多模型

我有2個數據模型,一個是user_details它看起來像這樣

public partial class user_details 
{ 
    public int id { get; set; } 
    public string username { get; set; } 
    public string pincode { get; set; } 
    public string mobilenumber { get; set; } 
    public int secq1id { get; set; } 
    public string secanswer1 { get; set; } 
    public int secq2id { get; set; } 
    public string secanswer2 { get; set; } 
} 

另一種是security_questions模型看起來像這樣

public partial class security_questions 
{ 
    public int id { get; set; } 
    public string security_question { get; set; } 
} 

我然後有一個控制器,它看起來像這樣

public class NewUserProfileViewModel 
{ 
    public IEnumerable<security_questions> Questions { get; set; } 
    public IEnumerable<user_details> EditableDetails { get; set; } 
} 

public class ProfileController : Controller 
{ 
    private readonly CCLPasswordManagementDBEntities passwordManagementConnection = new CCLPasswordManagementDBEntities(); 

    public ActionResult Manage() 
    { 
     bool userfound = false; 
     var userItemsModel = passwordManagementConnection.View_registered_userdetails; 

     foreach (var item in userItemsModel.Where(item => item.username == User.Identity.Name)) 
     { 
      userfound = true; 
     } 
     if (userfound) 
     { 
      UserProfileInfoViewModel upi = new UserProfileInfoViewModel 
      { 
       RegisteredDetails = userItemsModel, 
       Questions = passwordManagementConnection.security_questions 
      }; 
      return View(upi); 
     } 
     return RedirectToAction("Register"); 
    } 

    public ActionResult Register() 
    { 
     NewUserProfileViewModel upi = new NewUserProfileViewModel 
     { 
      Questions = passwordManagementConnection.security_questions, 
      EditableDetails = passwordManagementConnection.user_details 
     }; 
     return View(upi); 
    } 

    [HttpPost] 
    public ActionResult Register(NewUserProfileViewModel newUserProfile) 
    { 
     if (ModelState.IsValid) 
     { 
      return RedirectToAction("Index", "Home"); 
     } 
     else 
     { 
      return View(newUserProfile); 
     } 
    } 

當它關係到寄存器操作我有一個像下面

@model ServiceDeskMVCTool.Controllers.NewUserProfileViewModel 
@{ 
    ViewBag.Title = "Register Account"; 
    Layout = "~/Views/Shared/_CCLLayoutStandard.cshtml"; 
} 

@section title {<h1>@ViewBag.Title</h1>} 
@section subtitle {<h3>Hi @User.Identity.Name You have not set up your profile yet</h3>} 

<div id="page-block" class="page-block-three row"> 
    <div class="col-lg-offset-3 col-lg-6"> 

     @using (Html.BeginForm()) 
     { 
      @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
      <fieldset> 
       <div class="form-group"> 
        <h5>Pincode</h5> 
        @Html.TextBoxFor() 
       </div> 
      </fieldset> 
      <div class="form-group"> 
       <h5>Pincode</h5> 
       <input class="form-control ccl-form input-md" id="textinput" name="textinput" placeholder="Enter a 6 digit pincode" type="text"> 
      </div> 
      <!-- Mobile Number --> 
      <div class="form-group"> 
       <h5>Mobile Number</h5> 
       <input class="form-control ccl-form input-md" id="textinput" name="textinput" placeholder="Enter your work mobile number" type="text"> 
      </div> 
      <!-- Security Question 1 --> 
      <div class="form-group"> 
       <h5>Security Question 1</h5> 
       <select class="form-control ccl-form" id="selectbasic" name="selectbasic"> 
        @foreach (var item in Model.Questions) 
        { 
         <option value="@item.id">@item.security_question</option> 
        } 
       </select> 
       <input class="form-control ccl-form input-md form-padding" id="textinput" name="textinput" placeholder="Enter your security answer" type="text"> 
      </div> 
      <!-- Security Question 2 --> 
      <div class="form-group"> 
       <h5>Security Question 2</h5> 
       <select class="form-control ccl-form" id="selectbasic" name="selectbasic"> 
        @foreach (var item in Model.Questions) 
        { 
         <option value="@item.id">@item.security_question</option> 
        } 
       </select> 
       <input class="form-control ccl-form input-md form-padding" id="textinput" name="textinput" placeholder="Enter your security answer" type="text"> 
      </div> 
     } 

     <button style="margin-top: 10px" class="btn ccl-btn ccl-btn-md ccl-btn-red pull-right" type="button">Register</button> 
    </div> 
</div> 

我所試圖做的是使一些Html.TextboxFor但是當我通過Html.TextboxFor(型號=> model.editabledetails視圖.pincode)這不起作用

我對此很新,所以請輕鬆點!

謝謝!

回答

2

您需要在視圖模型中有一個單一的對象,而不是IEnumerable,以便綁定到...嘗試類似這樣的操作。

視圖模型

public class NewUserProfileViewModel 
{ 
    public IEnumerable<security_questions> Questions { get; set; } 
    public IEnumerable<user_details> EditableDetails { get; set; } 

    public user_details UserDetail { get; set; } 
} 

在你看來

@Html.TextBoxFor(x => x.UserDetail.pincode) 
+0

這是真棒解決了最初的問題,如果我可以更多的問題。 – 2014-11-03 20:44:06

+0

我需要在列表框中提取選定的安全問題並將其放置在模型中,我該怎麼做? – 2014-11-03 20:44:45

+0

只需更改您的選擇列表的名稱屬性,以匹配您的視圖模型,如'name ='UserDetail.secanswer1''等等 – mituw16 2014-11-03 20:52:48

0
@model ServiceDeskMVCTool.Controllers.NewUserProfileViewModel 
@{ 
    ViewBag.Title = "Register Account"; 
    Layout = "~/Views/Shared/_CCLLayoutStandard.cshtml"; 
} 

@section title {<h1>@ViewBag.Title</h1>} 
@section subtitle {<h3>Hi @User.Identity.Name You have not set up your profile yet</h3>} 

<div id="page-block" class="page-block-three row"> 
    <div class="col-lg-offset-3 col-lg-6"> 

     @using (Html.BeginForm()) 
     { 
      @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 

      <fieldset> 
       <div class="form-group"> 
        <h5>Pincode</h5> 
        @Html.TextBoxFor(x => x.UserDetail.pincode, new { @class = "form-control ccl-form input-md", placeholder = "Enter a 6 digit pincode" }) 
       </div> 
       <div class="form-group"> 
        <h5>Mobile Number</h5> 
        @Html.TextBoxFor(x => x.UserDetail.mobilenumber, new { @class = "form-control ccl-form input-md", placeholder = "Enter your work mobile number" }) 
       </div> 
       <div class="form-group"> 
        <h5>Security Question 1</h5> 
        <select class="form-control ccl-form" id="selectbasic" name="UserDetail.secq1id"> 
         @foreach (var item in Model.Questions) 
         { 
          <option value="@item.id">@item.security_question</option> 
         } 
        </select> 
        @Html.TextBoxFor(x => x.UserDetail.secanswer1, new { @class = "form-control ccl-form input-md form-padding", placeholder = "Enter your security answer" }) 
       </div> 
       <div class="form-group"> 
        <h5>Security Question 2</h5> 
        <select class="form-control ccl-form" id="selectbasic" name="UserDetail.secq2id"> 
         @foreach (var item in Model.Questions) 
         { 
          <option value="@item.id">@item.security_question</option> 
         } 
        </select> 
        @Html.TextBoxFor(x => x.UserDetail.secanswer2, new { @class = "form-control ccl-form input-md form-padding", placeholder = "Enter your security answer" }) 
       </div> 
       <input type="submit" style="margin-top: 10px" class="btn ccl-btn ccl-btn-md ccl-btn-red pull-right" value="Register" /> 
      </fieldset> 
     } 
    </div> 
</div> 

這似乎是幹活。

謝謝你的幫助。