I am trying to pass a strongly types my Model from view to Controller. My view is strongly typed. Somehow i am getting “null” in the property values when the “Save” action method is called in the controller. I am using Asp.Net MVC 3.
This is how my View looks like:
@model MvcApplication2.Models.Event
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>AddNew</title>
</head>
<body>
@using(Html.BeginForm("Save","Event",FormMethod.Post, Model))
{
<div>
<p>@Html.LabelFor(m=>m.EventName) @Html.TextBoxFor(m=>m.EventName)</p>
<p>@Html.LabelFor(m=>m.Venue) @Html.TextBoxFor(m=>m.Venue)</p>
<p>@Html.LabelFor(m=>m.StartTime) @Html.TextBoxFor(m=>m.StartTime)</p>
<p>@Html.LabelFor(m=>m.EndTime) @Html.TextBoxFor(m=>m.EndTime)</p>
@Html.ActionLink("Save Event", "Save")
</div>
}
</body>
</html>
This is how my EventController looks like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;
namespace MvcApplication2.Controllers
{
public class EventController : Controller
{
public string Save(Event eventModel)
{
//Here eventModel.EventName and rest of the properties are null.
return "Saved";
}
}
}
This is how Model looks like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication2.Models
{
public class Event
{
public string EventName { get; set; }
public string Venue { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
}
}
ActionLinks don’t submit forms. Change:
To
Additionally, this would have been more pronounced if you added
[HttpPost]to your method.