Hi guys,
Before diving into my actual question I would like to ask things which still aren't clear to me.
As you may notice ,all of MVC examples based on passing complex objects from to view to controller include strongly-typed views
I simply create a view,a model and a controller as follows
My view
@{
ViewBag.Title = "Home Page";
}
@using (Html.BeginForm("Indexs", "Home"))
{
<p>Name: <input type="text" name="name" /></p>
<p>Surname: <input type="text" name="surname" /></p>
<input type="submit" value="Login" />
}
My Model
public class Employee
{
public string name { get; set; }
public string surname { get; set; }
}
My controller
[HttpPost]
[ActionName("Indexs")]
public ActionResult Index(Employee asda)
{
return View();
}
If you notice I don't include any model binding stuff in my view.However the view is passing values as Employee object flawlessly.Don't we have to include something like below ??
@model MvcApplication4.Models.Employee
When I change only my model from properties to values and keep other parts same like below
public class Employee
{
public string name;
public string surname;
}
It simply doesn't work anymore.Briefly,I am getting null values when I am debugging.However If I change my controller as below and keep other code same , it is getting to work again but sure this time not for model
public ActionResult Index(string name,string surname)
{
return View();
}
In conclusion,
1-In order to pass complex objects to controller we don't need to make any view strongly typed to model but need properties instead of values
2-Simple types both work for properties and values
When I come to my actual question,
I have a controller called "Home" with a post action "Index" which takes an object "Employee" and view "Index" strongly typed to Employee object.
[HttpPost]
public ActionResult Index(Employee abc)
{
return PartialView("_customerpartialview",abc);
}
<pre lang="SQL"> [HttpGet]
public ActionResult Index()
{
Employee asd = new Employee();
asd.name = "example";
asd.surname = "example";
return View(asd);
}
Partial view resides in Index View,in the partial view I have a submit button too, which routes(using ajax) again same Action(Index Post).However model becomes null.Any idea about that ??
Thanks in advance