我的父视图如下
parent view
@{
project.Models.CustomerRegistration CustReg = ViewBag.CustReg;
project.Models.ForgetPassModel ForgotPassword = ViewBag.ForgotPassword;
project.Models.LoginModel Login = ViewBag.Login;
project.Models.LocalPasswordModel localpassword = ViewBag.localpassword;
}
@Html.Partial("Registration", CustReg, ViewData)
@Html.Partial("_Login", Login)
@Html.Partial("_ForgotPassword", ForgotPassword, ViewData)
注册parial
using (Html.BeginForm("RegistrationCustomer", "Account", FormMethod.Post))
{
@*@Html.ValidationSummary()*@
<div>
<h3>First Name</h3>
@Html.TextBoxFor(model => model.FirstName, new { @class = "form- control" })
@Html.ValidationMessageFor(m => m.FirstName)
@Html.HiddenFor(model=>model.ReferalId)
<h3>EmailId</h3>
@Html.TextBoxFor(model => model.UserName, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.UserName)
<h3>Password</h3>
@Html.TextBoxFor(model => model.Password, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Password)
<input type="submit" value="Submit" class="btn-3"/>
}
登录部分
@using (Html.BeginForm("RegistrationCustomer", "Account", FormMethod.Post))
{
@Html.ValidationSummary(true)
<h3>Email ID</h3>
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
<h3>Password</h3>
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
<input type="submit" value="Log in" class="btn-3"/>
if (Request["returnUrl"] == "Guest Login Failed")
{
<label>Incorrect UserId or Password</label>
}
}
forgotpassword partial
@using (Html.BeginForm("ForgotPassword", "Customer", FormMethod.Post))
{
<h2>@ViewBag.Role</h2>
<p>Please enter your UserName to get the login details</p>
<h3>Email Id</h3>
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
<div class="buttons-set">
<input type="Submit" value="Submit" class="btn-3"/>
}
mycontrollers
public ActionResult Login(string returnUrl,string role)
{
ViewBag.ReturnUrl = returnUrl;
return View();
ViewBag.ReturnUrl = returnUrl;
ViewBag.Role = role;
Session[SessionKeys.PreviousPage] = Convert.ToString(Request.UrlReferrer);
if (returnUrl == "Guest Login Failed")
{
ModelState.AddModelError("UserName1", "The user name provided is incorrect.");
return PartialView();
}
else
{
return PartialView();
}
}
[HttpPost]
public ActionResult Login(LoginModel log, string returnUrl, string role)
{
string Url = Request.UrlReferrer.ToString();//Session[SessionKeys.PreviousPage].ToString();
////string Url = "www.RekoMart.com";
string a = log.UserName;
log.URL = Url;
LoginModel Login = ul.ValidateLogin(log);
if (Login != null)
{
if (Login.LoginType.ToLower() == "customer")
{
FormsAuthentication.SetAuthCookie(Login.userId, true);//cust.RememberMe);
Session[SessionKeys.CurrentCustomer] = Login;
Session[SessionKeys.CurrentUSerId] = Login.GUESTID;
Session[SessionKeys.URL] = Url;
Session[SessionKeys.CUSTOMERUSERID] = a;
Session[SessionKeys.LoginTypeID] = Login.LoginTypeID;
Session[SessionKeys.UserId] = a;
Session[SessionKeys.ISCUSTOMER] = "True";
Session[SessionKeys.status] = "True";
Session[SessionKeys.UserRole] = "customer";
Session[SessionKeys.CUSTOMERID] = Login.CUSTOMERID;
//int cartProductsCount = 0;
//if (Session[SessionKeys.cartProductsCount] != null)
//{
// cartProductsCount = Convert.ToInt16(Session[SessionKeys.cartProductsCount].ToString());
//}
//if (Session[SessionKeys.PreviousPage] != null)
// return Redirect(Session[SessionKeys.PreviousPage].ToString());
//else
// if (cartProductsCount > 0)
// return RedirectToAction("Purchasecart", "Customer");
// else
return RedirectToAction("Index", "Home");
}
else
{
ViewBag.ReturnUrl = returnUrl;
ViewBag.Role = role;
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", " provided is incorrect.");
//return RedirectToAction("Login", new { returnUrl = "Guest Login Failed", role = "Customer" });
return RedirectToAction("RegistrationCustomer", new { returnUrl = "Guest Login Failed", role = "Customer" });
}
}
else
{
ViewBag.ReturnUrl = returnUrl;
ViewBag.Role = role;
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", " provided is incorrect.");
return PartialView(log);
}
}
public ActionResult Registration(int ReferalId)
{
CustomerRegistration cr = new CustomerRegistration();
cr.ReferalId = ReferalId;
return PartialView(cr);
}
[HttpPost]
public ActionResult Registration(CustomerRegistration model)
{
model.ReferalId = 0;
ICollection<ModelState> er = ModelState.Values;
int x = er.Where(a => a.Errors.Count > 0).Count();
int id=0;
UserAccountService uas = new UserAccountService();
if (ModelState.IsValid)
{
// Attempt to register the user
try
{
model.SponcerId = "0";
CustomerRegistration cust = uas.RegisterCustomer(model);
if (cust != null)
{
model.Message = "Successful";
Response.Write("Successful");
return PartialView(model);
}
else
{
ModelState.AddModelError("RegistrationCustomer", "Registration Failed");
model.Message = "Registration Failed";
return PartialView(model);
}
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
else
{
return RedirectToAction("RegistrationCustomer", model);
}
return PartialView(model);
}
我在空白注册表单上单击提交时收到此错误。传递到字典中的模型项的类型为“project.Models.CustomerRegistration”,但此字典需要“project.Models.LoginModel”类型的模型项。
答案 0 :(得分:1)
当您尝试绑定到强类型部分视图的模型为null时,会出现该错误。在页面内渲染此局部视图时,如果将空模型传递给@Html.Partial(...)
方法,则渲染器默认为发送当前页面的模型。
你的问题是在这个动作中(我假设它对应于你上面的父视图)你这样做:
public ActionResult Registration(int ReferalId)
{
CustomerRegistration cr = new CustomerRegistration();
cr.ReferalId = ReferalId;
return PartialView(cr);
}
但在你看来,你这样做:
@{
project.Models.CustomerRegistration CustReg = ViewBag.CustReg;
project.Models.ForgetPassModel ForgotPassword = ViewBag.ForgotPassword;
project.Models.LoginModel Login = ViewBag.Login;
project.Models.LocalPasswordModel localpassword = ViewBag.localpassword;
}
ViewBag不是一些神奇的空对象生成器。这是一个字典,您可以在一个请求的持续时间内存储与模型无关的数据。除了不首先填充ViewBag之外,您遇到的主要问题是您使用return PartialView(...)
呈现所有视图。此呈现方法会忽略您在ViewBag中完全设置的任何内容。因此,为了解决您的问题,我们需要进行一些小的修改,假设您的Login和ForgotPassword partials的模型应该是空的。
@{
// delete the custReg, let the partial inherit the model from the parent
project.Models.ForgetPassModel ForgotPassword = new project.Models.ForgetPassModel();
project.Models.LoginModel Login = new project.Models.LoginModel();
project.Models.LocalPasswordModel localpassword = new project.Models.LocalPasswordModel();
}
@Html.Partial("Registration")
@Html.Partial("_Login", Login)
@Html.Partial("_ForgotPassword", ForgotPassword)