行,所以我有以下情形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)這不起作用
我對此很新,所以請輕鬆點!
謝謝!
這是真棒解決了最初的問題,如果我可以更多的問題。 – 2014-11-03 20:44:06
我需要在列表框中提取選定的安全問題並將其放置在模型中,我該怎麼做? – 2014-11-03 20:44:45
只需更改您的選擇列表的名稱屬性,以匹配您的視圖模型,如'name ='UserDetail.secanswer1''等等 – mituw16 2014-11-03 20:52:48