[英]Extending interface with new model
I have a loginmodel where a user has to fill in a username and password but I wat to extend that model with a new model so that only a username is required. 我有一个loginmodel,用户必须填写用户名和密码,但我想用新模型扩展该模型,以便只需要一个用户名。 So this is my new model:
所以这是我的新模型:
public class V_LoginModel_BalieUser:LoginModel
{
public string BalieCode { get; set; }
}
And the original model looks like this: 原始模型看起来像这样:
//
// 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; }
}
and the new view looks like this: 并且新视图如下所示:
@{
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>
in the ProfileController I have these two methods: 在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();
}
This Interface: VI_ExtendedShopApiState_BalieUser looks like this: 此接口:VI_ExtendedShopApiState_BalieUser如下所示:
public interface VI_ExtendedShopApiState_BalieUser: IUserStateApi
{
bool LoginBalieUser(string username);
}
And in the Interface IUserStateApi has for example this method : 在接口IUserStateApi中有例如这个方法:
bool Login(string username, string password, bool persistent);
So I have extended this INterface with a new method(That I have declared above). 所以我用一种新的方法扩展了这个INterface(我在上面已经声明过)。
But now in this method: 但是现在用这种方法:
[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();
}
I get this error: 我收到此错误:
Use of unassigned local variable 'balieUser' 使用未分配的局部变量'balieUser'
But so my question is: what I have to assign to: 但我的问题是:我必须分配给:
VI_ExtendedShopApiState_BalieUser balieUser;
Thank you 谢谢
if I do it like this: 如果我这样做:
[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();
}
I get still the error: 我仍然得到错误:
Code Description Project File Line Column Suppression State CS0165 Use of unassigned local variable 'balieUser' 代码描述项目文件行列抑制状态CS0165使用未分配的局部变量'balieUser'
if I do it like this: 如果我这样做:
[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();
}
I get this error: 我收到此错误:
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?)
Shouldn't you be cause you are just declaring the type variable and not instantiating it at all as can be seen below 你应该不是因为你只是声明类型变量而根本没有实例化它,如下所示
VI_ExtendedShopApiState_BalieUser balieUser;
Moreover I doubt on the posted error message cause the below code line should throw a System.NullReferenceException
此外,我怀疑发布的错误消息导致下面的代码行应该抛出
System.NullReferenceException
balieUser.LoginBalieUser(model.BalieCode)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.