Quite new to MVC so please bear with me. I'm trying to load a partial view in a modal from a different controller and everytime I try to view it I get just an empty modal. I believe it is because I haven't been able to instantiate my model in the view.
E.g. I have a Controller 'Home' with a method 'Details' that returns a partialview from a different View folder. The native model to the controller is 'model', whereas the model belonging to my other controller is 'model2'.
public ActionResult Details() {
model2.User = user; //this is a global variable
model2.GetDetails();
return PartialView("~/Views/...Details", model2);
}
I'm sure the reason is because i'm missing the model data in the view. I tried adding another #model... to the view but clearly this doesn't work.
Is there a way of doing what I am trying to accomplish? It can even be a relatively dirty solution as this is a stopgap solution for the time being.
Reading back over this post it reads a little convoluted so if any clarification is needed please let me know.
Thanks
I faced this problem once before and i think it's a lot of work to reproduce it to provide an exact solution, but I can offer my 2 cents. The thing with browser Modals is that you need to provide a url when you are opening it. The URL will have to be the Controller/Action url and this is the tricky part which causes the problem. If you can figure that out, you should be able to solve the problem. If you can't, you can do one of the following:
1. Set the HTML content of Modal dialog from your main window's JS code after the Modal is opened.
2. Use one of the 3rd party HTML/CSS modal implementation, and set the HTML content from the JS code. In this case there is no browser modal and everything is on the same page.
To verify if the view is returning correctly from XHR, put the actionRoute URL in the browser address bar and you will see the content getting returned. It will help with troubleshooting.
Related
Working on MVC5 web app.
I have a view based on a complex viewmodel. In the main GET method I'm filling this viewmodel, setting certain ViewBag items, etc.... I'll refer to this as the "load code" for now.
So, my view is basically a data entry form. Business rules are fairly complex. When the user hits the save button I do a POST and run some c# code which checks rules, etc... If it FAILS, I simply want to "abort" the action and show the user the same GET view.
My question involves all that was done to initially load the view: The ViewBag code, filling of viewmodel, etc.... Do I have to write this "load code" again in my POST? This seems very redundant. Is there a way I can just "abort" the ActionResult/POST and show the GET view?
I hope this makes sense. Thanks!
**** update/clarification - based on comments ****
I suppose this question is more of a "general" one. Can you "abort" the POST and go "back" to the GET View without re-running all the "load code" (for the dropdowns, populating the viewmodel, etc....)
If you want to go to server and validate and then decide if you want to accept the POST or Fails/Abort and keep the previous page with all the values
then I'll suggest you to handle this in AJAX.
Create a third action/API Method and keep your validation logic there. then on the form submit hit the validation action using ajax and server will return True/False (with any error message) if you get True from server then let the javascript post your form otherwise show any error message and return false to stop posting the form and stay on the page.
For example use a javascript method to validate.
<form onsubmit="return isValidForm()" />
function
function isValidForm(){
//// hit the server and see if you want to proceed with the POST
//// if fails return false so your form will not be posted and all of your values will remains the same, here before return false you can clear the form if needed.
}
let me know if you need more information.
I've been searching for an answer to this question for a while now, I was hoping someone could give me clarity:
If I do a post from a partial View (or a called #Html.Action()) then there is no way save for usage of client-side scripting to have the application preserve state?
phrased differently: I can not post from a partial and just have it "do that part" and have the rest of the controllers remain as they were (provided no unsaved forms ofc)? Again with no java-script or the like.
What happens for me is that when I process the Http.Post and return a partial view, then that is the entirety of what gets written to the output pipeline and everything else in the browser vanishes.
I'm "solving" this right now by passing along a return-URL so I can redirect as an actionresult rather than send a partial view at the conclusion of the Http.Post processing. Is this the best I can do?
Use Ajax to update your partial view
this Question can be helpful for you
Update partial view after submit Ajax.Beginform
I'm developing a new website for my employer which contains a lot of CMS-style features. One of them is being able to create pages, specify URLs, menus, etc.
This is all fine.
What I'm after is a way to create a "Preview" button on these pages, that does not write to the database. I want to post (preferably the entire Model, if not, just the form data) to a new window and have that view render the page.
I have searched high and low and I cannot find an example that makes sense. Most people seem to have given up :/
I've attempted this by myself with TempData, however TempData gets cleared before I can render the page (as there is more than 2 steps involved from what I can deduce..).
I must POST as the form data will contain HTML (inside a Telerik Editor control).
Does anyone have any idea on a nice way to accomplish this? Or can anyone provide links to some resources? (I've come up completely blank!)
Regards,
chem
You can store the model in Session State.
I would store the data in session state.
this may be useful:
http://davidhayden.com/blog/dave/archive/2011/02/09/SessionLessControllersMvc3.aspx
You can still use TempData. At first step you need explicitly tell leave value in Session, for example:
string messageValue = (string) TempData.Peek("message"); // Does not cause ejection
You could serialize the model to XML and then the action method for the preview page could deserialize it.
See this link.
Thanks for the help guys/gals.
I managed to use TempData.
I basically ajax post to an action that stores the content in TempData using a Guid as the key. The Guid is returned and once the ajax post returns a hidden form with a target="_blank" action posts to a Preview action method passing through the Guid. That preview action then renders the view with the content in TempData.
Thanks for the suggestions.. it got me thinking!
Regards,
chem
I have a HomeController with an action About. I want to achieve this behavior:
Case #1 User open the page /home/about and the view render fully (return View();)
Case #2 User is on the homepage and clicks a link about, I want to change the url using history.pushState and render it partially (return PartialView();)
How can I achieve this behavior?
You can use Request.IsAjaxRequest to detect whether the controller action was invoked using an Ajax request.
As an alternative I'd recommend splitting your views so into smaller pieces, such that your About.aspx view uses the partial view, e.g. by doing Html.RenderPartial( "AboutBox" ). This gives you the power of reuse without having to clutter your actions with if-sentences.
yet another.
Restrict ASP.NET MVC Action's Using The ActionMethodSelectorAttribute
select action method control at ActionMethodSelectorAttribute.
I am posting a form to url /Admin/EditInPlace from page /About
In EditInPlace's conroller code i can find the Url-Referrer i.e. from which page request is coming. But how can i map the Url-Referrer to its View page??
In short how can i found that a page /xyz is mapping to which View.aspx file programmatically?
Needing to know what View the user was previously looking at smells like a design problem. As a workaround, consider passing a query string parameter to the Admin controller's EditInPlace method that describes which UI the user just saw.
/Admin/EditInPlace?channel=basic