开发者

ASP.Net MVC Form-post and jquery View not updated

I'm using asp.net mvc with jquery. I'm trying to make a formpost and just update the ascx that making the post.

I'm making the post with following code:

$(document).ready(function() {
    $('#search-form').s开发者_运维知识库ubmit(function() {
        var form = $('#search-form');
        var action = form.attr('action');
        var serializedForm = form.serialize();

        $.post(action, serializedForm, function() {

        });
        return false;
    });
});

Then I have an action:

[HttpPost]
public ActionResult Method(FormCollection collection)
{
    //code...
    ViewData["post"] = "Hello world";
    return View();
}

This is working fine. The code is executing, but the problems occur after return view. For example if I try to print out the "Hello world" in the view it is never updated. Could someone explain why and do anyone know about a solution?


When you're making an AJAX request like this, all you have access too in the return is the result returned from the View producted by the action method, not the full data storage associated with the controller.

If you wanted to display the "Hello world" text then you'd have to either write it out to the page as part of your view or include it in the output response.

Response.Write("Hello World");

You don't have access to the ViewData or TempData collections after your view has returned. It sounds like that's what you're expecting.


I suggest that you make ajax.form in a partial view, and do

<div id="DIVtoUpdate"> Render.Partial("PartialViewName",model);</div>

and inside of your partial view u got

using (Ajax.BeginForm("*ActionName*", new { *parameter = ID* }, new AjaxOptions { UpdateTargetId = "DIVtoUpdate", OnSuccess = "*JavaScript that executes on success*",  OnComplete = "s*ame as on success*", InsertionMode = InsertionMode.Replace })) 
{ 
    /*REST OF YOUR VIEW*/ 
}

so when you do post and controller action executes you should return

return PartialView("PartialViewNAme",model);

and your div will get updated with the new data. (note: disable ajax:cache, in some cases you can have problems with it)


asp.net will be expect either a view with the same name of your action or you should explicitly tell which view you are returning finally. For example : return View("ViewName");

ViewData is a dictionary based collection that will be available in the client side similar to the ViewState.

You have to get it casted and should make use.

for example

in the view page

String _data =(String)ViewData["post"];

then if you want display in your page

<span>@_data</span>

or

@{

<span>_data</span>

}

see another example here you can iterate and can generate dynamic html also. MVC providing a better option for dynamic controls like this.

@{
 IList<items> itemlist=(IList<items>)ViewData["yourString"];
   foreach(item in itemlist )
  {

    <label>item</label> <br/>

  }
}

you probably will get output like
item1
item2
item3
etc..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜