ASP.NET MVC: Why postback on partial view loaded using ajax returns null model?
-
My application have page with few search boxes. When user clicks on respective search button a result grid is expected to load. That grid have few editable controls in which user can modifications and hit save button if needed to save the data. We have implemented the functionality using AjaxForm and partial view. 1.Search area containing search boxes and search button is created using Ajax.BeginForm, on submit of which a postback Search method is called from controller. SearchModel is passed to this method. 2.Partial View is created to show results and Ajax form in step 1 loads it successfully from controller postback method. SearchModel.Results property is passed to view as its model. 3.!This partial view showing results have a Save button (again a Ajax form) which invokes another method in controller but gets model null in controller. have tried lot of tricks but was unsuccessful. Any working example demonstrated anywhere or suggestion to make this working ? There are lot of examples on web where usage of AjaxForm to load data is explained but didnât found any for multiple (or nested?)
-
Answer:
This happens when your view sends incompatible types to the model, so the object passed to the controller is null. Most certainly what you need to do is drop that Model and replace it with a ViewModel. These two should not be confused, even though the naming is similar. To more precisely answer this question you must provide details on what exactly this means: few search boxes .. as it's not really clear. 95% you have a dropdownlist in there, that breaks the model sent to the controller. Dropdownlists require a viewmodel, just a model won't do.
Andrei Cristof at Quora Visit the source
Other answers
This question is quite old and difficult to understand; however, I believe I may know the actual answer to the question. If JavaScript is enabled, then you can test IsAjaxRequest in the controller action and refresh the dependent elements with script. (When JavaScript is not enabled, it doesn't matter.) I have a similar situation where there are two ajax forms on a page. The upper form contains a create form with a Save button. The lower form contains a list with edit and delete links. To prevent the page from losing state, which causes the model funk you may be describing, I refresh the list like this: at bottom of controller action for top form: if (Request.IsAjaxRequest()) { if (ModelState.IsValid) { model = new ResourceCategory(); Response.AddHeader("RefreshList", "true"); } return PartialView("~/views/admin/_ResourceCategoryEdit.cshtml", model); } if (ModelState.IsValid) { return RedirectToAction("ResourceLibrary", "Admin"); } return View(model); at bottom of delete or other controller actions for dependent ajax form: if (Request.IsAjaxRequest()) { Response.AddHeader("RefreshList", "true"); return new EmptyResult(); } return RedirectToAction("ResourceLibrary", "Admin"); ajax form definition for edit form: @model Crowdit.DataAccess.ResourceLibrary.ResourceCategory @using(Ajax.BeginForm("ResourceCategoryEdit", "Admin", new AjaxOptions() { HttpMethod = "post", OnComplete="refreshCategoryList", OnSuccess="resetOnComplete", UpdateTargetId = "categoryedit", InsertionMode = InsertionMode.Replace }, new { @class="autoreset" })) { @Html.HiddenFor(m => Model.CategoryId) .... ajax form definition for dependent element or list: @helper Commands(Crowdit.DataAccess.ResourceLibrary.Resource item) { @Ajax.ActionLink("edit", "resourcecategoryitemsedit", "admin", new { editId = item.ResourceId }, new AjaxOptions() { HttpMethod = "post", OnComplete = "refreshCategoryItemsList", UpdateTargetId = "categoryitemsedit", InsertionMode = InsertionMode.Replace }) <text> </text> @Ajax.ActionLink("del", "resourcecategoryitemsdelete", "admin", new { id = item.ResourceId }, new AjaxOptions() { HttpMethod = "post", OnComplete = "refreshCategoryItemsList" }) <text> </text> } JavaScript included in the main page that contains the two partials: @section scripts { <script> function refreshCategoryList(data) { if (data.getResponseHeader('RefreshList')) { $.post('@Url.Action("resourcecategorylist", "admin")', function (data) { $('#categorylist').html(data); }); } } </script> } Synopsis: When the ajax form used to edit or create new is posted, a header is added in the controller action telling the script to refresh the list. And, consequently, when edit or delete action links are posted, the controller actions do the same. The result is that each partial is kept in-sync and their models are "fixed up". Both partials have different types for their models, one is IEnumerable<T> and the other is T. The helper inserts ajax action links for each item in a table. When JavaScript is disabled, the whole page is refreshed instead.
Markus Pope
Can you please show some of your code in order to suggest you an answer . From all your info I think you are using main view With Partial View in it . I Suggest you To Post some of your code its must to see some of your code .If you got some problem with modal . I suggest you to use View Modals they are very easy and best to use if you are having problem with your ajax call you need to properly debug your ajax calls and provide info whether they are giving any error etc
Himanshu Bhalla
Related Q & A:
- How can I allow user to create posts in website using ASP.NET?Best solution by Programmers
- How to save high resolution image canvas to server using asp.net?Best solution by Stack Overflow
- How to use Authorize Attribute in asp.net MVC?Best solution by Stack Overflow
- How to use angular.js with strongly typed view in asp.net-MVC?Best solution by ojdevelops.com
- Is there some kind of API for MSN protocol (for using in C# asp.net application?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.