0

Hi I am new to MVC and I have the below senario

VIEW:

step3.cshtml is my view

This has data binded to step3.js(using knockout)

 $.post("/Step/Step4", { "some":some_data }, function (data) {


            }, 'JSON');

CONTROLLER:

     [HttpPost]
        public ActionResult Step4(model foo)
        {
            //save this data to the database and return a view 
            using (DBContext dbContext = new DBContext())
            {
                dbContext.Table.Add(foo);
                dbContext.SaveChanges();

            }
            return View(foo);

        }

        public ActionResult Step4()
        {
            //get this view with the model

        }

I am able to see that the view Step4.cshtml is able get the property values from model but. I see that my View Step4.cshtml is a response header from the post how do I render this in the browser.

Is this the right approach??

So is it that an HTML form post can return a view and replace the whole content and AJAX cannot do that except for the partial view update?

sss111
  • 349
  • 1
  • 7
  • 27

1 Answers1

1

When you call $.post, you are making a jQuery ajax request with a POST method. That data is given back to you in the success method. If you are returning the view, then you will have to manually replace the html in your document with that response.

Normally, when you use ajax like this, you would return a PartialView to update a section of your page, not your whole page. You might want to see if you can return a partial and update a div.

You can also do that by using an Ajax.ActionLink, and in the AjaxOptions, specify the id of the element you want to update when the response is returned, and the InsertionMode, which should be set to Replace. And you won't have to worry about replacing the content yourself.

Please see this stackoverflow question, which may help you: How To Use Ajax.ActionLink

Community
  • 1
  • 1
Eric
  • 930
  • 1
  • 6
  • 14
  • So is it that an HTML form post can return a view and replace the whole content and AJAX cannot do that except for the partial view update? – sss111 Jul 11 '14 at 20:47
  • Usually developers use ajax to update a section of their page, rather than the whole page. By returning a view to your ajax call, you now have a string of html that represents the entire page. You would have to replace all the content in your page. If you return a partialview, you are just getting a section of html that you can update inside a div or something – Eric Jul 11 '14 at 21:01