Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi I am trying to pass information to my controller.
This is my jquery function:
JavaScript
$("#Add").click(function () {
            var jsonText = JSON.stringify({ tagName: getPTag(), start: getPStart(), end: getPEnd() });
            $.ajax({
                url: '@Url.Action("Data", "TagValues")',
                dataType: 'JSON',
                data: jsonText,
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    alert(data.toString());
                },
                error: function () {
                    alert("Tag or Times aren't formatted right");
                }
            });
        });


This is in the RouteConfig:
C#
routes.MapRoute(
                "TagValuesdata",
                "TagValues/Data/{tagName}/{start}/{end}",
                new { controller = "TagValues", action = "Data", tagName = "", start = "", end = "" }
                );


This is in my controller:
C#
public class TagValuesController : Controller
    {
        //
        // GET: /TagValues/

        public JsonResult Data(string tagName, string start, string end)
        {
            return Json(tagName, behavior: JsonRequestBehavior.AllowGet);
}


When ever I run the function. I get an alert message that is blank. Why is my parameter an empty string or null? Is it a routing problem? I tested my getPTag() with an alert, and getPTag returns the string.
Posted

1 solution

Here is simple Ajax with post. Assuming that you do have an html table with a name called AjaxGrid and a div with a name bio

JavaScript
$(document).ready(function () {
        $('#AjaxGrid tbody tr').click(function () {
            var selectedraw = $(this).closest("tr");
            var id = selectedraw.find("td:first").text(); // Id value
            var url = "FamousFolks/GetFamousFolk/" + id;
            $.post(url,
                   null,
                   function (folk) {
                       $('#bio').html(folk.Bio);
                   }
            );
        });

HTML
<table id="AjaxGrid">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.ID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>           
        </tr>
    </thead>
    @foreach (var item in Model)
    {
        <tr>
            <td style="visibility: hidden">
                @Html.DisplayFor(modelItem => item.ID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>           
        </tr>
    }
    <td>
        <div id="bio">
        </div>
    </td>
</table>

And here is the controller.
C#
/// <summary>
/// Get Bio of Folk
/// </summary>
/// <param name="id">Id Value</param>
/// <returns></returns>
[HttpPost]
public ActionResult GetFamousFolk(int? id = null)
{

    // Folk a database table mapped to EF entity with ID,Name and Bio attributes and comes from the model
    // Folks is a collection of Folk class
    Folks folk = folksRepository.Load(id); // Load folk by Id
    return Json(folk);// Get Folk data as JSON format
}

And register the routing like this
C#
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");            
    routes.MapRoute(
      "FamousFolks", // Route name
      "{controller}/{action}/{id}", // URL with parameters
      new { controller = "FamousFolks", action = "Index", id = UrlParameter.Optional } // Parameter defaults
  );          
}
 
Share this answer
 
v2
Comments
Member 8336910 28-Jun-12 10:25am    
I used your id idea and your register routing id idea. It worked! Thanks much
--Sean
Wonde Tadesse 28-Jun-12 19:43pm    
Thanks

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