Take the 2-minute tour ×

The question here is very simple

This is my view

<%@ Control Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<GetmoreRevamp.BAL.Product>" %>
<link href="<%=Url.Content("~/Content/AddToCart.css")%>" rel="stylesheet"
      type="text/css" />
<link href="<%=Url.Content("~/Scripts/jquery-1.4.1.js")%>" type="text/javascript" />

<script type="text/javascript">
    function submitForm(formData) {
        var tdata = $(formData).serialize();
            type: "POST",
            url: '<%= Url.Action("AddToCart","Cart")%>',
            data: tdata,
            datatype: "json",
            success: function(result) { success(result); }
        return false;
    function success(result) {
        alert("success:" + result.success);

<% using (Html.BeginForm("AddToCart", "Cart ", Model, FormMethod.Post,
             new { onsubmit = "return submitForm('this');" })) {%>
<div class="prishosbtn">
    <a rel="prettyPhoto" href="" id="buy">
        <%Response.Write("<input type=\"image\" class=\"imgClass\" alt=\"" +
                         (Model != null && Model.ProductName != null ?
                                       Model.ProductName : "KOEB") + "\" src=\"" +
                        Url.Content("~/pics/undersider/listevisning/kob-knap.png") +
                          "\" id=\"ImageButton\" name=\"ImageButton\" />");%>

This is my controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GetmoreRevamp.WEB.Models;
using GetmoreRevamp.WEB.Models.BLLModels;
using System.Web.Security;
using System.Security.Principal;
using GetmoreRevamp.WEB.Utilities;
using GetmoreRevamp.BAL;

namespace GetmoreRevamp.WEB.Controllers
    public class CartController : Controller
        // GET: /Cart/
        public ActionResult Index()
            return View("Cart");

        public ActionResult AddToCart(Product product)
            JsonResult result = new JsonResult();
            OrderHeader orderHeader = 
                Session[Constants.CurrentlySessionOrderHeader] as OrderHeader;
            if (orderHeader == null)
                orderHeader = new OrderHeader();
            if (product != null && product.ProductGuid != null &&
                string.Equals(product.ProductGuid, string.Empty))
                orderHeader.AddOrderLineItem(1, product);
                Session[Constants.CurrentlySessionOrderHeader] = orderHeader;
                //Default redirection Must be changed when actual view is created
                result.Data = true;
                //Default redirection Must be changed when actual view is created
                result.Data = false;
            return result;

"Product" is defined in bal. Product contains other business entities. What i simply want to do is to access the model with which view is binded in jquery and then post it to my action method in cart controller. i do not want to post the id of product. I want to post the actual model via jquery to my action method. I am a total newbie in this. so any help and most imp simple solution will be preferred

share|improve this question

1 Answer 1

MVC matches field names to the business object in the action method, so if Product has a ProductID field, there should be a:


declaration, or use the TextBoxFor method in MVC 2. I'm pretty sure that's still how it works even when posting using JQuery. The model binder handles the process of taking the form fields and passing them to the product object. But, all of the fields have to be within the form that you are posting to the server, or you have to explicitly pass the parameters where you pass the tdata variable...


share|improve this answer
Brian my question is that in jquery how can i get the actual model object with which view is bound. Suppose there is a strongly typed view a with model class b. now we can display or edit the fields of an instance of b via html elements in a. which is straight forward. If an action method d of contoller c accepts b as a parameter we can postback/redirect/get to that action method.that is easy. My scenario is bit of an extension to it. Since a is submitting an instance of b to d as a parameter it means it either has stored an instance of b in it or it knows how to create an instance of b (cont) –  Syed Salman Akbar Jun 22 '10 at 8:04
(Continuing from previous) from information available to it. What i want to do is to access that instance of b in jquery inside a serialize it if necessary and then submit it via json to an action method in any contoller that accepts an instance of b as a parameter. –  Syed Salman Akbar Jun 22 '10 at 8:08
What I'm trying to say is that the model binding process doesn't post an object, when using any approach. It posts the fields of the object to the server. On the server, the model binding process uses reflection to match the textbox with a name of ProductID to the product ID of the property of the object stored within the model reference. MVC actually has a flexible binding architecture as it can also work with nested objects. You always have at your disposal the ability to accept a FormCollection as the action method param and then use TryUpdateModel to push values to the object. –  Brian Mains Jun 22 '10 at 12:17
So MVC doesn't utilize a client-side component of a server-side object like it generates with web service proxies in web forms, it just utilizes a flexible binding architecture to match fields to property names. –  Brian Mains Jun 22 '10 at 12:19

Your Answer


