【问题标题】:Extending interface with new model新模型扩展接口
【发布时间】: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


    【解决方案1】:

    你不应该是因为你只是声明类型变量而不是实例化它,如下所示

           VI_ExtendedShopApiState_BalieUser balieUser;
    

    此外,我对发布的错误消息表示怀疑,因为下面的代码行应该抛出 System.NullReferenceException

    balieUser.LoginBalieUser(model.BalieCode)
    

    【讨论】:

    • 感谢您的回复。但是实例化?它是一个接口。所以我当然不能实例化 VI_ExtendedShopApiState_BalieUser 。还是你的意思是别的?
    • @mightycodeNewton,我的意思是实例化一个实现你的这个交互的具体类型
    • 我这样尝试:VI_ExtendedShopApiState_BalieUser balieUser = new V_LoginModel_BalieUser();
    • ?但是如何改进呢?
    • 那我做错了什么?有人可以帮助我吗?谢谢
    猜你喜欢
    • 2010-09-05
    • 2021-08-07
    • 2018-04-09
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 2015-08-09
    • 2021-08-07
    • 2021-02-06
    相关资源
    最近更新 更多