微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我所有的SelectList都返回空值而不是实际值

如何解决为什么我所有的SelectList都返回空值而不是实际值

我有一个MVC5项目,其中有多个SelectList下拉列表。它们似乎在视图中起作用(我的意思是所有选择都出现在下拉列表中),但是在提交时,它们始终返回null而不是应有的值。由于该值为空,因此将导致异常。我尝试通过几种不同的方式进行此操作,但无法弄清楚如何获取此值以返回实际值。它通过了验证,但仍返回null。这是我的代码

这是我的控制者:

public ActionResult Create()
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["UserName"])))
            {
                return RedirectToAction("Login","Home");
            }
            /* var mymodel = new LoginSecurityQuestionsviewmodel();
             mymodel.logins = db.logins.ToList();
             ViewBag.securityquestions = db.secretquestions.ToList();
             ViewBag.churches = db.churches.ToList();
             ViewBag.subscriptionTypes = db.subscriptiontypes.ToList();
             ViewBag.musicianTypes = db.musiciantypes.ToList();
             return View(mymodel);*/
            LoginSecurityQuestionsviewmodel viewmodel = new LoginSecurityQuestionsviewmodel();
            viewmodel.chlist1 = new SelectList(db.churches,"id","ChurchName");
            viewmodel.chlist2 = new SelectList(db.churches,"ChurchName");
            viewmodel.chlist3 = new SelectList(db.churches,"ChurchName");
            viewmodel.chlist4 = new SelectList(db.churches,"ChurchName");
            viewmodel.seclist = new SelectList(db.secretquestions,"Question");
            viewmodel.subscriptionTypes = new SelectList(db.subscriptiontypes,"StripePriceID","SubscriptionType1");
            viewmodel.musicianTypes1 = new SelectList(db.musiciantypes,"TypeName");
            viewmodel.musicianTypes2 = new SelectList(db.musiciantypes,"TypeName");

            return View(viewmodel);
        }

        // POST: login/Create
        // To protect from overposting attacks,enable the specific properties you want to bind to,for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        public ActionResult Create(LoginSecurityQuestionsviewmodel chargeDto)
        {
            
            if (ModelState.IsValid == false)
            {
                LoginSecurityQuestionsviewmodel viewmodel = new LoginSecurityQuestionsviewmodel();
                viewmodel.chlist1 = new SelectList(db.churches,"ChurchName");
                viewmodel.chlist2 = new SelectList(db.churches,"ChurchName");
                viewmodel.chlist3 = new SelectList(db.churches,"ChurchName");
                viewmodel.chlist4 = new SelectList(db.churches,"ChurchName");
                viewmodel.seclist = new SelectList(db.secretquestions,"Question");
                viewmodel.subscriptionTypes = new SelectList(db.subscriptiontypes,"SubscriptionType");
                viewmodel.musicianTypes1 = new SelectList(db.musiciantypes,"TypeName1");
                viewmodel.musicianTypes2 = new SelectList(db.musiciantypes,"TypeName1");

                return View(viewmodel);
            }
            
            StripeConfiguration.ApiKey = "sk_test_51HCsISKAzhBUyKohSD72jQsQC6JmP68nOJ4yo0PyoLcw6xq3kZN7AXOy8oVMVEwJvG1tlWse0zM5ljFIiFz5H7eP00Pc84FqDR";

            //Create a Customer
            var customerOptions = new CustomerCreateOptions
            {
                Name = chargeDto.CardName,Source = chargeDto.Stripetoken,Email = chargeDto.Email,Phone = chargeDto.Phone,Address = new AddressOptions
                {
                    Line1 = chargeDto.Address,Line2 = chargeDto.Address2,City = chargeDto.City,State = chargeDto.State,PostalCode = chargeDto.Zip,},Metadata = new Dictionary<string,string>
                {
                }
            };
            var customerService = new CustomerService();
            Customer customer = customerService.Create(customerOptions);

            // `source` is obtained with Stripe.js; see https://stripe.com/docs/payments/accept-a-payment-charges#web-create-token
            /*var options = new ChargeCreateOptions
            {
                Amount = 2000,Currency = "usd",//Source = chargeDto.Stripetoken,Description = "My First Test Charge (created for API docs)",Customer = customer.Id
            };*/

            var options = new SubscriptionCreateOptions
            {
                Customer = customer.Id,Items = new List<SubscriptionItemOptions>
                {
                    new SubscriptionItemOptions
                    {
                        Price = chargeDto.Plan,TrialPeriodDays = 30,};


            var service = new SubscriptionService();
            Subscription subscription = service.Create(options);

            var model = new Chargeviewmodel();
            model.ChargeId = subscription.Id;
            model.CustomerId = subscription.CustomerId;
            login newUser = new login();
            newUser.AccountPasscode = (int)chargeDto.Pin;
            newUser.AccountType = chargeDto.Plan.ToString();
            newUser.Address = chargeDto.Address;
            newUser.CellPhone = chargeDto.Cell;
            newUser.ChurchAffiliation = chargeDto.ChurchAffiliation1;
            newUser.ChurchAffiliation2 = chargeDto.ChurchAffiliation2;
            newUser.ChurchAffiliation3 = chargeDto.ChurchAffiliation3;
            newUser.ChurchAffiliation4 = chargeDto.ChurchAffiliation4;
            newUser.City = chargeDto.City;
            newUser.Company= chargeDto.Company;
            newUser.Country = "USA";
            newUser.lastlogin = DateTime.MinValue.ToString();
            newUser.LastMonthReset = DateTime.MinValue.ToString();
            newUser.LastYTDReset = DateTime.MinValue.ToString();
            newUser.MoneyEarnedMonth = 0;
            newUser.MoneyEarnedYTD = 0;
            newUser.MusicianType = chargeDto.MusicianType;
            newUser.MusicianType2 = chargeDto.MusicianType2;
            newUser.Name = chargeDto.CardName;
            newUser.Password = chargeDto.Password;
            newUser.Phone = chargeDto.Phone;
            newUser.ProfilePicture = "None";
            newUser.SecretAnswer = chargeDto.Answer;
            newUser.SecretQuestion = chargeDto.SecurityQuestion;
            newUser.SignupDate = DateTime.Now;
            newUser.State = chargeDto.State;
            newUser.SubscriptionExpiration = DateTime.MinValue.ToString();
            newUser.SubscriptionStatus = 0; //Do not provision access until Stripe charge clears.
            newUser.UserName = chargeDto.Email;
            if(chargeDto.Plan == "price_1HOnxHKAzhBUyKohQTIndEzD")
            {
                newUser.UserType = 1;
            }
            if (chargeDto.Plan == "price_1HCsfAKAzhBUyKohfqJ1J0yO")
            {
                newUser.UserType = 2;
            }
            if (chargeDto.Plan == "price_1HOnzlKAzhBUyKohZFNq7FRo")
            {
                newUser.UserType = 3;
            }
            newUser.Zip = Convert.ToInt32(chargeDto.Zip);
            newUser.StripeCustomerID = customer.Id;

            
            db.logins.Add(newUser);
            db.SaveChanges();
            return RedirectToAction("Index");

            


        }
}

这是我的viewmodel:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Database_Access;

namespace Church_Musician_Administration_App__Updated_.Models
{
    public class LoginSecurityQuestionsviewmodel
    {
        [required(ErrorMessage = "Please select a security question")]
        [display(Name = "Security Question")]
        public int? seclistid { get; set; }
        
        public IEnumerable<SelectListItem> seclist { get; set; }


        [display(Name = "Church Affiliation 1")]
        public int? chlist1id { get; set; }


        public IEnumerable<SelectListItem> chlist1 { get; set; }


        [display(Name = "Church Affiliation 2")] 
        public int? chlist2id { get; set; }

        
        public IEnumerable<SelectListItem> chlist2 { get; set; }

        
        [display(Name = "Church Affiliation 3")] 
        public int? chlist3id { get; set; }

        
        public IEnumerable<SelectListItem> chlist3 { get; set; }

        
        [display(Name = "Church Affiliation 4")]
        public int? chlist4id { get; set; }
        
        
        public IEnumerable<SelectListItem> chlist4 { get; set; }

        
        public login logins { get; set; }
        
        
        [required(ErrorMessage = "Please select a plan")]
        [display(Name = "Plan")]
        public string planid { get; set; }
        
        
        public IEnumerable<SelectListItem> subscriptionTypes { get; set; }
        
        
        [display(Name = "Musician Type 1")]
        [required(ErrorMessage = "Please select a musician type you would like to get work for")]
        public int? musicianType1id { get; set; }
        
        public IEnumerable<SelectListItem> musicianTypes1 { get; set; }
        
        
        [display(Name = "Musician Type 2")]
        [NotEqual("musicianType1id",ErrorMessage ="Musician Type 2 cannot be the same as Musician Type 1")]
        public int? musicianType2id { get; set; }
        
        
        public IEnumerable<SelectListItem> musicianTypes2 { get; set; }
        
        
        [required]
        [RegularExpression("^[A-Za-z]+$",ErrorMessage = "This field can only contain letters")]
        public string FirstName { get; set; }
        
        
        [required]
        [RegularExpression("^[A-Za-z]+$",ErrorMessage = "This field can only contain letters")]
        public string LastName { get; set; }
       
        
        [required]
        [RegularExpression("^[a-zA-Z_]+( [a-zA-Z_]+)*$",ErrorMessage = "This field can only contain letters")]
        public string Name { get; set; }


        public string Company { get; set; }


        [required]
        [DataType(DataType.Password)]
        [StringLength(20,MinimumLength = 8)]
        [RegularExpression(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,15}$",ErrorMessage = "The password must match the requirements below")]
        public string Password { get; set; }
        
        
        [required]
        [DataType(DataType.Password)]
        [display(Name = "Confirm password")]
        [System.ComponentModel.DataAnnotations.Compare("Password",ErrorMessage = "Passwords must match")]
        public string ConfirmPassword { get; set; }

        
        public int SecurityQuestion { get; set; }

        
        public string Answer { get; set; }

        
        public int? Pin { get; set; }
       
        
        public int ChurchAffiliation1 { get; set; }
       
        
        public int ChurchAffiliation2 { get; set; }
       
        
        public int ChurchAffiliation3 { get; set; }
        
        
        public int ChurchAffiliation4 { get; set; }

        
        public string Plan { get; set; }

        
        public string CardName { get; set; }

       
        public string Email { get; set; }

        public string Phone { get; set; }
        
        
        public string Cell { get; set; }

        
        public int MusicianType { get; set; }
        
        
        public int MusicianType2 { get; set; }

        
        public string Address { get; set; }
        
        
        public string Address2 { get; set; }

        
        public string City { get; set; }

        
        public string State { get; set; }


        public string Zip { get; set; }


        public string Stripetoken { get; set; }

    }



    public class security_questions_list
    {
        public int id { get; set; }
        public string Question { get; set; }
    }

    public class church_list
    {
        public int id { get; set; }
        public string ChurchName { get; set; }
        public string ChuchDenomination { get; set; }
        public string ChurchType { get; set; }
        public string ChurchCity { get; set; }
        public string ChurchState { get; set; }
           
    }

    public class subscription_type
    {
        public int id { get; set; }
        public string SubscriptionType1 { get; set; }
        public int Price { get; set; }
        public string StripePriceID { get; set; }
    }

    public class musician_type
    {
        public int id { get; set; }
        public string TypeName { get; set; }
    }
}

这是我的视图的示例(我的所有下拉菜单都这样)

<div class="form-group">
                        <label for="plan" class="control-label">Select a Plan</label><br />
                        @Html.DropDownListFor(model => model.planid,Model.subscriptionTypes,"None",new { @id = "plan",@class = "form-control" })
                        @Html.ValidationMessageFor(model => model.planid,"",new { @class = "text-danger" })
                    </div>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?