Why is PartialView not called when being called from another ActionResult method? - asp.net-mvc-3

Im using MVC4.
I have a [HttpPost] controller method that returns an ActionResult.
In this function, when I catch an exception, my plan is to call a [ChildActionOnly] ActionResult method which returns PartialView("ExceptionMessage", exceptionObject).
ExceptionMessage.cshtml basically renders a markup with the full Exception object.
Now for some reason, when this PartialView is invoked, the breakpoint within the .cshtml is not being hit.
Why does this not work?

It was a user error on my part.
The view is called when the controller method returns, and thus gets a chance to render the cshtml.

Related

async Dynamic partial view using action

System.Web.HttpException: 'Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.'
InvalidOperationException: HttpServerUtility.Execute blocked while waiting for an asynchronous operation to complete.
my partail view codemy render controller methode
My get method that is async to call api
i tried to access render partial view but fails i want dynamic async partial view using #html.Action or any other

Play Framework 2.x : Ajax request to controller without page refresh

I have a controller LessonController with an action save(). My route for this action is POST /save LessonController.save(name: String). The code is very simple for this action.
public static Result save(String name){
Lesson lesson = new Lesson();
lesson.setTitle(name);
lesson.save();
return ok(lesson.getLessonId().toString());
}
Here is my ajax call from the view
myJsRoutes.controllers.LessonController.save(name).ajax({
success : function(id){
alert('Success');
}
});
This code does create a lesson but it refreshes the page and does not execute the success block from ajax request. I guess it is because I am returning a Result type back. I tried changing the return type to String but it gave me a compilation error.
So the question is, How can I call an action in a controller with ajax without the page refresh behaviour?
If you get a page refresh from that then it is for some other reason. Like for example triggering the ajax call with a submit button and not consuming the button event so that it leads to a form submit POST or GET
Please check you AJAX call event, may but you are triggering the AJAX call using submit button or may be you are submitting any form in the web page with the same event.

MVC3 RenderPartial and the ViewBag

I'll try to keep this brief:
In the view
#Html.RenderPartial("myview", Model.SubModel,
new ViewDataDictionary()
{
{ "thing", Model.Thing }
})
In the myview we see that "thing" is avaiable, i.e. this produces the value of Model.Thing in the myview view:
#ViewBag.thing
Great! Simple
But I need to do something with thing (and thing can't be part of SubModel by the way), i.e. access this in my view engine or ideally in the controller, e.g.:
public ActionResult myview(SubModelType vm)
{
var thing = ViewBag.thing; // oh dear this doesnt exist.. but is there when the view is rendered
So my question is, if the ViewBag is available in the resulting myview, it must be being passed around somehow in the httpcontext, or in the controllercontext somewhere right? Does anyone know why it's not available in the controller but is in the view and how I might be able to access this?
Edit
Sorry I missed a vital point here! It's when "myview" is posted back to the controller, invoking an action called myview where I would expect the ViewBag rendered to myview to be available. But of course it's not, it's been used in "myview" and that's it. So if I want to use it in the myview action I'm going to need to store in the ViewBag in that view OR set some value of the viewmodel so that it can be posed back to the action.
Does that make sense?
Sorry I missed a vital point here! It's when "myview" is posted back
to the controller, invoking an action called myview where I would
expect the ViewBag rendered to myview to be available.
Oh no, you cannot be possibly expecting anything like that. That's not how ASP.NET MVC works. When you send an HTTP request to a controller action the default model binder will intercept this request and look in the posted values from the request and bind them to your action argument:
[HttpPost]
public ActionResult myview(SubModelType vm, ThingViewModel thing)
{
...
}
This obviously assumes that those thing values were part of the original request. So for example if you submit a form you should include corresponding fields inside this form that the model binder could use.
Think of it that way: an ASP.NET MVC controller action could be invoked from any client. For example from an iPhone application. And as you know there's no such notion as ViewBag in iOS. All that will happen is that the default model binder will look at the POSTed values and attempt to hydrate the view models that your action is taking as arguments.
If on the other hand you cannot make those thing values as part of the request (by including corresponding input fields in the form) you could only send the ID of this thin from a hidden field and then inside your controller action use this ID to query your underlying data-store to retrieve the thing from the same place you retrieved it initially when you first rendered this form.
Some people might also suggest you storing the Thing inside the Session and then reading the value back from the Session in your POST action. I am not from those people. It's an alternative approach though.

asp.net mvc 3 jquery or ajax issue with #Ajax.BeginForm()

I am working on a simple CMS project, and i have run into a wall. And then after a lot of hours, blew it up.
For some reason my buttons inside #Ajax.BeginForm were not activating the postback. Neither full postback nor ajax. I have referenced the scripts, and tried to debug jQery, examined every character in the Controllers, Views, References and Models to no avail.
There were no errors or exceptions thrown.
Controller action:
[HttpPost]
public ActionResult Delete(int ID,int page=1)
{
//some code...
//...work, work, work...
}
View segment:
#using (Ajax.BeginForm("Delete", "Client", new { page = Model.CurrentPageIndex }, ao))
{
#Html.Hidden("clientID", item.ClientID)
<input type="submit" value="BriĊĦi" name="brisi" />
}
Error:
The parameters dictionary contains a null entry for parameter 'ID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Delete(Int32, Int32)' in 'Info3CRM.WebUI.Controllers.ClientController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
Finally i have decided to disable jQuery and Ajax and do normal postback. Then the exception was thrown. I was trying to call the Action in controller without all the necessary parameters.
My question to all is: How to catch such an exception if jQuery and ajax are enabled?
You could see this error in some kind of client side debugging tool.
For example, Fiddler would show you the response that came back from the server - and you can use Fiddler's WebView tab to see the HTML response. The HTML response in this case would show you the ASP.NET yellow error screen.
In addition to Fiddler, you could also use browser debugging tools that come built into IE,, Chrome or Opera - or download Firebug for FireFox. Each tool has a network tab which will show you all the ajax requests and their responses.
Your problem is that the action you are POSTing to expects to find a parameter named ID, while there is no such parameter being POSTed. If you change the name of the hidden input to match the name of the parameter it should all work fine.
So, change
#Html.Hidden("clientID", item.ClientID)
to
#Html.Hidden("ID", item.ClientID)

Call an Action from an MVC View

I'm trying to figure out how to call an action from an MVC view, not call a view URL.
I have an Action Method for signing off in the Account Controller:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SignOff()
{
authenticationService.SignOut();
return View("SignOff");
}
And I'd like to call it from a cshtml page, like this:
<li>#Html.Action("SignOff","Account")</li>
The list is part of a drop down menu.
ActionLink doesn't work because I don't have an Account/SignOff page. The above code returns the following: "Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'." ActionLink just tells me the resource doesn't exist.
Is there anyway to do this? I just want users to be able to select a "Logout" item from a menu and then have the application call the SignOff method. Do I need to use Ajax? Javascript? I'm still experimenting. Thank yoU!
EDIT: Ok, it was the http.post that was keeping me from calling this directly. I got rid of the acceptverbs attribute and now I can call the method directly.
You can just redirect to the Login page or to any other public page (home page).
return RedirectToAction("Login");

Resources