MVC表单:-下拉列表:-发布表单获取错误“对象引用未设置为对象的实例”

时间:2018-07-10 12:09:03

标签: asp.net-mvc post dropdown sitecore-mvc

使用MVC形式的下拉列表并提交时,出现错误“对象引用未设置为对象的实例”。 如果删除下拉字段,则表单提交成功。

Controller.cs

  [RedirectAuthenticated]
        public ActionResult Register()
        {

            var model = this.UserProfileService.GetEmptyProfileRegistrationInfo();
            return this.View(model);
        }

 public virtual RegistrationInfo GetEmptyProfileRegistrationInfo()
        {
            return new RegistrationInfo
            {
                InterestTypes = this.profileSettingsService.GetInterests()
            };
        }

[HttpPost]
        [ValidateModel]
        [RedirectAuthenticated]
        [ValidateRenderingId]
        public ActionResult Register(RegistrationInfo registrationInfo)
        {
            if (!string.IsNullOrEmpty(registrationInfo.Interest) && !this.UserProfileService.GetInterests().Contains(registrationInfo.Interest))
            {
                this.ModelState.AddModelError(nameof(registrationInfo.Interest), DictionaryPhraseRepository.Current.Get("/Accounts/Edit Profile/Interest Not Found", "Please select an interest from the list."));
                registrationInfo.InterestTypes = this.UserProfileService.GetInterests();
            }
            if (!this.ModelState.IsValid)
            {
                return this.View(registrationInfo);
            }


            if (this.AccountRepository.Exists(registrationInfo.Email))
            {
                this.ModelState.AddModelError(nameof(registrationInfo.Email), UserAlreadyExistsError);

                return this.View(registrationInfo);
            }


            try
            {
                this.AccountRepository.RegisterUser(registrationInfo, this.UserProfileService.GetUserDefaultProfileId());

                var link = this.GetRedirectUrlService.GetRedirectUrl(AuthenticationStatus.Authenticated);
                return this.Redirect(link);
            }
            catch (MembershipCreateUserException ex)
            {
                Log.Error($"Can't create user with {registrationInfo.Email}", ex, this);
                this.ModelState.AddModelError(nameof(registrationInfo.Email), ex.Message);

                return this.View(registrationInfo);
            }
        }

型号

namespace Sitecore.Feature.Accounts.Models
{
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using Sitecore.Feature.Accounts.Attributes;
    using Sitecore.Foundation.Dictionary.Repositories;

    public class RegistrationInfo
    {
        [Display(Name = nameof(InterestsCaption), ResourceType = typeof(RegistrationInfo))]
        public string Interest { get; set; }

        public IEnumerable<string> InterestTypes { get; set; }


        public static string InterestsCaption => DictionaryPhraseRepository.Current.Get("/Accounts/Edit Profile/Interests", "Interests");
    }
}

我无法解决问题,我已经初始化并将页面加载上带有DD“列表值”的模型传递到Register()ActionResult中。

@using Sitecore.Foundation.Dictionary.Extensions
@using Sitecore.Foundation.SitecoreExtensions.Extensions
@using Sitecore.Mvc.Configuration
@model Sitecore.Feature.Accounts.Models.RegistrationInfo

@using (Html.BeginRouteForm(MvcSettings.SitecoreRouteName, FormMethod.Post, new{ @class = "form-signin"}))
{
    @Html.AddUniqueFormId()



    <div class="form-group @Html.ValidationErrorFor(x => x.Interest, "has-error")">
        @Html.LabelFor(x => x.Interest, new
                                        {
                                            @class = "control-label"
                                        })

        @Html.DropDownListFor(x => x.Interest, new SelectList(Model.InterestTypes), "", new
            {
                @class = "form-control",
                id = "InterestTypes"
            })

        @Html.ValidationMessageFor(x => x.Interest, "", new
                                                        {
                                                            @class = "help-block"
                                                        }, "p")
    </div>


    <input type="submit" class="btn btn-primary btn-lg btn-block" value="@Html.Sitecore().Dictionary("/Accounts/Register/Register", "Register")" />
}

enter image description here


尝试以下代码,但仍会出错

**[HttpPost]
        [ValidateModel]
        [RedirectAuthenticated]
        [ValidateRenderingId]
        public ActionResult Register(RegistrationInfo registrationInfo)
        {
            if (string.IsNullOrEmpty(registrationInfo.Interest) )
            {
                this.ModelState.AddModelError(nameof(registrationInfo.Interest), DictionaryPhraseRepository.Current.Get("/Accounts/Edit Profile/Interest Not Found", "Please select an interest from the list."));
                var model = this.UserProfileService.GetEmptyProfileRegistrationInfo();
                return this.View(model);
            }
            if (!this.ModelState.IsValid)
            {
                return this.View(registrationInfo);
            }


            if (this.AccountRepository.Exists(registrationInfo.Email))
            {
                this.ModelState.AddModelError(nameof(registrationInfo.Email), UserAlreadyExistsError);

                return this.View(registrationInfo);
            }


            try
            {
                this.AccountRepository.RegisterUser(registrationInfo, this.UserProfileService.GetUserDefaultProfileId());

                var link = this.GetRedirectUrlService.GetRedirectUrl(AuthenticationStatus.Authenticated);
                return this.Redirect(link);
            }
            catch (MembershipCreateUserException ex)
            {
                Log.Error($"Can't create user with {registrationInfo.Email}", ex, this);
                this.ModelState.AddModelError(nameof(registrationInfo.Email), ex.Message);

                return this.View(registrationInfo);
            }
        }**

0 个答案:

没有答案