The parameters dictionary contains a null entry for parameter 'membershipTypeId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult CustomerForm(Int32, System.String, Boolean, Int32)' in 'WebApplication2.Controllers.CustomerController'.An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
My customer class which has a many - to one relationship with membershipType class:
public class Customer
{
public Customer()
{
this.Movies = new HashSet<Movie>();
}
public int id { get; set; }
[Required]
[StringLength(255)]
public String Name { get; set; }
public bool IsSubscribedToNewsletter { get; set; }
public int CurrentMembershipTypeId { get; set; }
public MembershipType CurrentMembershipType { get; set;}
public ICollection<Movie> Movies { get; set; }
}
My MembershipType class:
public class MembershipType
{
public int Id { get; set; }
public short SignupFee { get; set; }
public byte DurationInMonths { get; set; }
public byte DiscountRate { get; set; }
public ICollection<Customer> Customers { get; set; }
// public virtual ICollection<Movie> Movie { get; set; }
}
My CustoemrController: get Function for customer form:
public ActionResult CustomerForm(Customer customer)
{
var membershipTypes = iMembershipTypeRepository.GetMembershipTypes();
ViewBag.Message = "Customers form is going to display: ";
if (customer.IsSubscribedToNewsletter != true)
ViewBag.Addon = "Selected";
else
ViewBag.Addon = "Not Selected";
return View(membershipTypes);
}
Post action for Customer form:
[HttpPost]
public ActionResult CustomerForm(int CustomerId, string CustomerName, bool CustomerisSubscribedToNewsLetter, int membershipTypeId)
{
Customer customer = new Customer();
customer.id = CustomerId;
customer.Name = CustomerName;
customer.IsSubscribedToNewsletter = CustomerisSubscribedToNewsLetter;
customer.CurrentMembershipTypeId = membershipTypeId;
iCustomerRepository.InsertCustomer(customer);
iCustomerRepository.Save();
return RedirectToAction("Customer");
}
CustomerForm view:
<h2>CustomerForm</h2>
<form action="/Customer/CustomerForm" method="post">
<table>
<tr>
<td>Id:</td>
<td><input id="CustomerId" type="text" name="CustomerId" /></td>
</tr>
<tr>
<td>Name:</td>
<td><input id="CustomerName" type="text" name="CustomerName" /></td>
</tr>
<tr>
<td>IsSubscribedToNewLetter:</td>
<td><input id="CustomerisSubscribedToNewsLetter" type="text" name="CustomerisSubscribedToNewsLetter" /></td>
</tr>
<tr>
<td>MembershipType:</td>
<td>
<select id="MembershipTypeId" name="MembershipTypes">
@foreach (var membershipType in @Model)
{
<option value=@membershipType.Id>
@*@membershipType.SignupFee*@
@membershipType.Id
</option>
}
</select>
</td>
</tr>
</table>
<input type="submit" value="save"/>
</form>