0

Model Binding is not working due to some reason in asp.net mvc. I am not sure where the mistake is.

This is the action method.

[HttpPost]
public ActionResult Create(NewCustomerViewModel model)
{
return View();
}

Below code is the view.

@model Vidly.ViewModels.NewCustomerViewModel
@{
    ViewBag.Title = "New";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>New Customer</h2>

@using (Html.BeginForm("Create", "Customers"))
{
    <div class="form-group">
        @Html.LabelFor(m => m.Customer.Name)
        @Html.TextBoxFor(m =>m.Customer.Name, new { @class = "form-control"})
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Customer.Birthday)
        @Html.TextBoxFor(m => m.Customer.Birthday, new { @class = "form-control" })
    </div>
    <div class="checkbox">
        <label>
            @Html.CheckBoxFor(m => m.Customer.IsSubscribedToNewsLetter, new { @class = "checkbox" }) Subscribed to Newsletter?
        </label>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Customer.MembershipTypeId)
        @Html.DropDownListFor(m => m.Customer.MembershipTypeId,new SelectList(Model.MembershipTypes,"Id", "MembershipTypeName"),"Select Membership Type", new { @class = "form-control" })
    </div>
   <button type="submit" class="btn btn-primary">Save</button>
}

Below is the View Model that i am using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Vidly.Models;

namespace Vidly.ViewModels
{
    public class NewCustomerViewModel
    {
        public IEnumerable<MembershipType> MembershipTypes;
        public Customer Customer;
    }
}

Membership Type Model is below -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Vidly.Models
{
    public class MembershipType
    {
        public byte Id { get; set; }
        public short SignUpFee { get; set; }
        public byte DurationInMonths { get; set; }
        public byte DiscountRate { get; set; }
        public string MembershipTypeName { get; set; }

    }
}

Customer Model is below -

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

namespace Vidly.Models
{
    public class Customer
    {
        public int Id { get; set; }
        [Required]
        [StringLength(25)]
        public string Name { get; set; }
        public bool IsSubscribedToNewsLetter { get; set; }
        public MembershipType MembershipType { get; set; }
        [Display(Name="Membership Type")]
        public byte MembershipTypeId { get; set; }
        [Display(Name="Date Of Birth")]
        public DateTime? Birthday { get; set; }
    }
}

I am not sure what is wrong in this code. Thanks for helping.

  • What is not working? (you have not even shown your POST method) –  Dec 03 '17 at 20:54
  • And view models do not contain data models, just the properties of your data models that you need in the view. –  Dec 03 '17 at 20:56
  • Yes. When I added , {get; set;} to the view model properties, it started working. looks like that was the issue. – Bharath Tatachar Dec 03 '17 at 21:18

0 Answers0