简体   繁体   中英

How to bind drop-down list to model

I learn ASP.NET and I try to bind my drop-down list to model, where user set some value in view, controller set this value to model that connect to database.

I've tried to use Request.Form, but it doesnt work for drop-down.

View

<form asp-action="Index" method="post">
            <p>
                Quantity of people:
                <select name="quantity" class="form-control">
                    <option>Quantity of people</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                </select>



            <p>
                Choose class of your apartment:
                @Html.DropDownList("Class",
                         new SelectList(Enum.GetValues(typeof(Classes))),
                         "Choose class",
                         new { @class = "form-control" })
                @Html.ValidationMessage("class")



            <p>
                Date of your arrival:
                <input id="startdate" class="form-control" type="date" name="start">


            <p>
                Date of your departure:
                <input id="enddate" class="form-control" type="date" name="end">


                @Html.ValidationSummary(true)

            <p>
                <input type="submit" value="Send" class="btn btn-default"   />
            </p>

        </form>

Model:

public class UserBook
    {
        public int UserBookId { get; set; }
        public int UserQuantity { get; set; }
        public UserClasses UserClass { get; set; }
        public DateTime UserDateTimeStart { get; set; }
        public DateTime UserDateTimeEnd { get; set; }


        public enum UserClasses
        {
            Econom,
            Standart,
            Luxury
        }

    }

In controller I understand that i need to do class instance like

 IRepository<Apartment> dbshow;
        IRepository<UserBook> dbrec;
        public HomeController()
        {
            dbshow = new BookingRepository();
            dbrec = new RequestRepository();
        }
 [HttpPost]
        public ActionResult UserRequest(UserClasses? Class, int? quantity, DateTime? start, DateTime? end, )
        {

            UserBook u = new UserBook();
            u.UserQuantity = quantity.GetValueOrDefault();
            u.UserClass = Class.GetValueOrDefault();
            u.UserDateTimeStart = start.GetValueOrDefault();
            u.UserDateTimeEnd = end.GetValueOrDefault();



            ViewData["CurrentQuan"] = quantity;
            ViewData["CurrentClass"] = Class;
            ViewData["CurrentStart"] = start;
            ViewData["CurrentEnd"] = end;




            if (quantity.HasValue)
            {

            }
            else { ModelState.AddModelError("", "Input number of people"); }

            if (Class.HasValue)
            {

            }
            else { ModelState.AddModelError("", "Input class of apartment"); }

            if (start.HasValue)
            {
            }
            else { ModelState.AddModelError("", "Введите дату заезда"); }

            if (end.HasValue)
            {
            }
            else { ModelState.AddModelError("", "Input arrival date"); }

            if (start < DateTime.Now || end < DateTime.Now)
            {
                ModelState.AddModelError("", "Date isn't correct");
            }

            if (start >= end)
            {
                ModelState.AddModelError("", "Date of arrival and departure isn't valid");
            }
            dbrec.Update(u);
            return View();
        } 

Thanks for all answers!

Try like this

 u.UserQuantity = quantity.GetValueOrDefault();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM