【发布时间】:2017-09-05 04:28:48
【问题描述】:
我有一个登录模型,用户必须填写用户名和密码,但我想用新模型扩展该模型,以便只需要用户名。 所以这是我的新模型:
public class V_LoginModel_BalieUser:LoginModel
{
public string BalieCode { get; set; }
}
原来的模型是这样的:
//
// Summary:
// A model to login into the webshop.
public class LoginModel
{
public LoginModel();
//
// Summary:
// Gets or sets the password.
[AllowHtml]
[DataType(DataType.Password)]
[Display(Name = "Password")]
[Required(ErrorMessageResourceName = "Validation_RequiredField")]
[StringLength(30, ErrorMessageResourceName = "Validation_MaxLengthExceeded")]
public virtual System.String Password { get; set; }
//
// Summary:
// Gets or sets a value indicating whether to remember the user to login him automatically
// on the next visit.
[Display(Name = "Login_RememberMe")]
public virtual System.Boolean RememberMe { get; set; }
//
// Summary:
// Gets or sets the username.
[DataType(DataType.EmailAddress, ErrorMessageResourceName = "Validation_InvalidField")]
[Display(Name = "EmailAddress")]
[Required(ErrorMessageResourceName = "Validation_RequiredField")]
[StringLength(80, ErrorMessageResourceName = "Validation_MaxLengthExceeded")]
[TrimAttribute(new[] { })]
public virtual System.String UserName { get; set; }
}
新视图如下所示:
@{
Layout = LayoutPaths.General;
}
@model Sana.Commerce.DomainModel.Account.V_LoginModel_BalieUser
<div class="semicolumn">
<div class="form-holder">
@using (Html.BeginForm(htmlAttributes: new { @class = "form" }))
{
@Html.AntiForgeryToken()
<table>
<tr>
<th>
@Html.DisplayNameFor(modelItem => modelItem.BalieCode)
</th>
<th></th>
</tr>
<tr>
<td>
@Html.TextBoxFor(modelItem => modelItem.BalieCode)
</td>
</tr>
</table>
<div class="form-row">
<h4></h4>
<input type="submit" value="Login" />
</div>
}
</div>
<div>
</div>
</div>
在 ProfileController 我有这两种方法:
[HttpGet]
public ActionResult LoginBalieUser()
{
return View();
}
[HttpPost]
public ActionResult LoginBalieUser(V_LoginModel_BalieUser model)
{
VI_ExtendedShopApiState_BalieUser balieUser;
ISalesPersonProfile salesAgent99 = CommerceFrameworkBase.SalesPersons.GetSalesPerson("HD");
if (!ModelState.IsValid)
return View(model);
if (!balieUser.LoginBalieUser(model.BalieCode))
{
ModelState.AddModelError("", "");
return View(model);
}
return View();
}
这个接口:VI_ExtendedShopApiState_BalieUser 看起来像这样:
public interface VI_ExtendedShopApiState_BalieUser: IUserStateApi
{
bool LoginBalieUser(string username);
}
并且在 IUserStateApi 接口中有例如这个方法:
bool Login(string username, string password, bool persistent);
所以我用一个新方法(我在上面声明的)扩展了这个接口。
但是现在在这个方法中:
[HttpPost]
public ActionResult LoginBalieUser(V_LoginModel_BalieUser model)
{
VI_ExtendedShopApiState_BalieUser balieUser;
ISalesPersonProfile salesAgent99 = CommerceFrameworkBase.SalesPersons.GetSalesPerson("HD");
if (!ModelState.IsValid)
return View(model);
if (!balieUser.LoginBalieUser(model.BalieCode))
{
ModelState.AddModelError("", "");
return View(model);
}
return View();
}
我收到此错误:
使用未赋值的局部变量“balieUser”
但是我的问题是:我必须分配给:
VI_ExtendedShopApiState_BalieUser balieUser;
谢谢
如果我这样做:
[HttpPost]
public ActionResult LoginBalieUser(V_LoginModel_BalieUser model)
{
VI_ExtendedShopApiState_BalieUser balieUser;
balieUser.LoginBalieUser(model.BalieCode);
ISalesPersonProfile salesAgent99 = CommerceFrameworkBase.SalesPersons.GetSalesPerson("HD");
if (!ModelState.IsValid)
return View(model);
if (!balieUser.LoginBalieUser(model.BalieCode))
{
ModelState.AddModelError("", "");
return View(model);
}
return View();
}
我仍然得到错误:
代码描述项目文件行列抑制状态 CS0165 使用未分配的局部变量“balieUser”
如果我这样做:
[HttpPost]
public ActionResult LoginBalieUser(V_LoginModel_BalieUser model)
{
//model = new V_LoginModel_BalieUser();
VI_ExtendedShopApiState_BalieUser balieUser;
balieUser = new V_LoginModel_BalieUser();
balieUser.LoginBalieUser(model.BalieCode);
ISalesPersonProfile salesAgent99 = CommerceFrameworkBase.SalesPersons.GetSalesPerson("HD");
if (!ModelState.IsValid)
return View(model);
if (!balieUser.LoginBalieUser(model.BalieCode))
{
ModelState.AddModelError("", "");
return View(model);
}
return View();
}
我收到此错误:
Code Description Project File Line Column Suppression State
CS0266 Cannot implicitly convert type 'Sana.Commerce.DomainModel.Account.V_LoginModel_BalieUser' to 'Sana.Commerce.Customization.Interfaces.VI_ExtendedShopApiState_BalieUser'. An explicit conversion exists (are you missing a cast?)
【问题讨论】:
标签: c# asp.net-mvc c#-4.0