Click here to Skip to main content
15,902,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Razor
@{
    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

C#
public class Employee
{
    public string name { get; set; }
    public string surname { get; set; }
}

My controller
SQL
[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

C#
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

SQL
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.


SQL
[HttpPost]
public ActionResult Index(Employee abc)
{
    return PartialView("_customerpartialview",abc);
}


C#
<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
Posted

1 solution

I solved the issue , the problem was that In my main view I used Ajaxhtmlhelper which was sending my model behind the scenes.In my partial view I used classic Ajax Call and forgot the send the data.So It made me understand that Ajaxhtmlhelper does creating model and serialization stuffs behind the scenes.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900