How to read the values returned by the Json?

Posted by user281180 on Stack Overflow See other posts from Stack Overflow or by user281180
Published on 2010-05-10T11:07:29Z Indexed on 2010/05/10 11:14 UTC
Read the original article Hit count: 149

Filed under:

I have the following code in my view:

 <% using (Ajax.BeginForm("JsonCreate", new AjaxOptions { OnComplete = "createCategoryComplete" }))

{ %> Add new category

<%=Html.TextBox("CategoryId")%> <%=Html.TextBox("Test")%> Name: <%= Html.TextBox("Name")%> <%= Html.ValidationMessage("Name")%>

        </p>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
</div>

<% } %>

In the controller the code is as follows:

[AcceptVerbs(HttpVerbs.Post)]
    public JsonResult JsonCreate(string Name)
    {
        if (ModelState.IsValid)
        {
            try
            {


                //Return a json object to the javascript
                return Json(new { CategoryId = 123, Test= "test successful" });
            }
            catch
            {
                #region Log errors about the exception
                //Log error to administrator here
                #endregion
            }
        }

        //If we got this far, something failed, return an empty json object
        return Json(new { /* Empty object */ });
    }

What should be the code in the view for the following function to read the values returned by the Json and update the textboxes for CategoryId and Test?

  function createCategoryComplete() {....???}

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2