1
我有一個loginmodel,用戶必須填寫用戶名和密碼,但是我希望使用新模型擴展該模型,以便只需要用戶名。 所以這是我的新模式:使用新模型擴展接口
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?)
謝謝你的回覆。但是實例化?它是一個接口。所以我不能實例化VI_ExtendedShopApiState_BalieUser。或者你的意思是別的嗎? –
@mightycodeNewton,我的意思是instanciating實現您的這種聯繫的具體類型 – Rahul
我編輯帖子 –