簡體   English   中英

擴展新模型的界面

[英]Extending interface with new model

我有一個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);

所以我用一種新的方法擴展了這個INterface(我在上面已經聲明過)。

但是現在用這種方法:

  [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 balieUser;

此外,我懷疑發布的錯誤消息導致下面的代碼行應該拋出System.NullReferenceException

balieUser.LoginBalieUser(model.BalieCode)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM