Testing Controller Action Which Returns RedirectToRouteResult?

May 19, 2010

I have an action in my controller:

RedirectToRouteResult Create(UserDTO UserDTO)

Which at some point decides with which HTML to respond after a post request by redirecting to an action:

return ModelState.IsValid ? RedirectToAction("ThanksCreate") : RedirectToAction("Register");

In my unit tests I would like to get hold of the 'views' modelstate somehow like this:

var modelState = result.ViewData.ModelState;
Assert.IsFalse( modelState.IsValid );

where 'result' (ViewResult) is the result of the action 'Create' depending on the submitted DTO. My dilemma is that my action 'returns' a RedirectToRouteResult which I thought is quite nice but it might not be testable or is it?

How could I get hold of the ModelState in my scenario?

View 1 Replies


Similar Messages:

Test That A Controller Action Simply Returns A Link To Another Action?

Apr 29, 2010

Lets say I have a simple controller for ASP.NET MVC I want to test. I want to test that a controller action (Foo, in this case) simply returns a link to another action (Bar, in this case).How would you test TestController.Foo? (either the first or second link)

My implementation has the same link twice. One passes the url throw ViewData[]. This seems more testable to me, as I can check the ViewData collection returned from Foo(). Even this way though, I don't know how to validate the url itself without making dependencies on routing.The controller:

public class TestController : Controller
{
public ActionResult Foo()[code].....

View 1 Replies

MVC :: Controller Invokes GET Action Instead Of POST Action

May 20, 2010

I'm trying to add file upload functionality to a page. I've got a form that posts the selected file to a controller with a 'savefile' method. But if I don't add a get version of 'savefile' I'll get a 404 error. Here is the form code which is presented on the Index page:

[Code]....

And here is the controller code:

[Code]....

Intuitively I don't think I should need a GET version of SaveFile but if omit it I get a 404 error when the form posts. Why should I need a GET version of SaveFile when all I want is to post a form and save the file?

View 4 Replies

MVC :: Get The Current Action / Controller Name In A Controller Or Class?

Mar 14, 2011

How do you get the current action / controller name in a controller or class?

i can't show it in my view but that's not what i want.

View 1 Replies

MVC :: Calling Action Of A Controller From Another Controller?

Jan 24, 2011

I have a User entity and a department entity. I have a UserViewModel and DepartmentListModel which has List of departments.

UserViewModel has a property of type DepartmentListModel .

Now on user/create action I need to populate DepartmentListModel by calling DepartmentController's list action. List action returns populated DepartmentListViewModel.

From UserController how do I set DepartmentListModel ?

I tried doing so

[Code]....

But RedirectToAction returns RediretToRouteResult

View 7 Replies

MVC :: Controller Testing - Operation Could Destabilize The Runtime

Dec 17, 2010

I am receiving System.Security.VerificationException. {"Operation could destabilize the runtime."}
Asp.net mvc 3 + Razor rc1 and rc2 and NUnit and MoQ.

After some research, I did see that similar error with TypeMock but it was fixed with Microsoft .

[URL]

The Test method is as below:

[code]...

View 2 Replies

MVC :: Set The Model From Controller Unit Testing Method?

Mar 24, 2011

I am using the Nhibernate with MVC. In the controller i am getting the model from the HttpSession. How can i set the model from controller Unit testing method.

View 1 Replies

MVC :: Controller Base Class Initialize() And OnActionExecuting() Not Called When Unit Testing?

Nov 16, 2010

I'm using a simple base class for my controller that sets a piece of ViewData and makes use of the HttpContext. My understanding is that I need to put this code in Initialize() or OnActionExecuting(). I tried both, but believe Initialize() is what I want.

This works when running the app in VS just fine.

I wrote a unit test that calls the action and checks the ViewData for the expected value. When unit testing, neither method is called.

View 2 Replies

MVC :: Null Action Id Returns System.NullReferenceException?

Oct 26, 2010

This is what I'm trying to run:

[Code]....

When I browse to www.mysite.com/mycontroller/myaction/myid, everything works ok. But visiting www.mysite.com/mycontroller/myaction produces:Server Error in '/' Application.Object reference not set to an instance of an object.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

View 3 Replies

MVC :: Action That Returns Viewresults For Ajax Client To Consume?

Dec 10, 2010

I have an action that will return partial view result if no error is found in the request , but when an error is found , how do I pass the error information back to client ?

Some error info now is in TempData["message"] , but some are model validation messages . The web client is using jQuery to make the ajax request (with $.ajax( ) call ) .

How does the jquery client distinguish between sucess or failure , and then acts appropriately to update a DIV that display TempData["message"] , and a DIV that consume the partial view result .

I was thinking perhaps a view-result can be encapsulated into a JSON result , and the JSON result will have fields to indicate the success/failure status , and the encoded markup of the partial view result .

View 3 Replies

How To Get The Id Value In Controller / Action / Id

Sep 24, 2010

I have an edit application view, and it can be found at the following URL:

http://localhost:17262/Application/EditApplication/1

1 equals the application ID.

Also on the page I have a link that goes to another view to add an assistant for the application. I want to pass it the application ID so that the new assistant can be "linked" to this application. How would I get the value of 1 and add it to my action link? This is what I have in my HTML so far:

<%: Html.ActionLink("Add New Assistant", "Create", "Assistant", new { applicationID = "id" }, null) %>

View 1 Replies

MVC :: One Action Per Controller?

Jul 9, 2010

If I have a controller with more than one action on, e.g. UserController, then I end up having that class require every dependency of every action. Is there a way I can have one controller per action and yet have the URLs appear the same way they currently do? e.g.MySite.com/User/Edit/1 would remap to User_EditController.Edit

View 1 Replies

Same Controller Action Being Invoked In Mvc

Jan 12, 2011

I am not sure what is going on but even though I am invoking a different controller with action a particular FooController's Index action is being invoked all the time. The Global.asax file has setup the FooController to be the default controller.

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "FooController", action = "SomeAction", id = UrlParameter.Optional } // Parameter defaults
);
}

View 1 Replies

.net - Aspx Url To Mvc Controller Action

Dec 17, 2010

I have an aspx page Test.aspx. It handles requests like

Test.aspx?First=value1&Second=value2&third=value3

How can I use routing to redirect this url to

TestController/MyAction?First=value1&Second=value2&third=value3

I know I can create an aspx and perform redirect in it`s page load. But seems ugly and I think it can be done with some custom route.What I`ve tried was: this solution.but it didnt work for me. I remember, that Test.aspx should not be on a disk. I don`t have it, and routing is still not working.

View 1 Replies

C# - Securing Controller Action In MVC?

Feb 2, 2010

In ASP.NET MVC 2, to secure controller action, i have created a class RequirePermission inherited from ActionFilterAttribute class. The controller action looks like

[Code]....

Now instead of making different attributes , I want to use RequirePermission attribute like
[RequirePermission(permissions=Permissions.CanView+","+Permissions.CanEdit)] so that i can use it for different scenerious. but the compiler throw the following error. An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

View 1 Replies

MVC :: How To Get An Action Link From Within The Controller

Dec 21, 2010

I know I can use Html.ActionLink(...) from my view to render an anchor tag with a link to an action. I know I can call RedirectToAction(...) from the controller to immediately call another action. But what I'd like to do (and don't know how), is get an action link from within the controller. I am building up a breadcrumb and want the link to an action. So I don't want to immediately jump to the action (as with RedirectToAction), but just get what the link would be.

View 3 Replies

C# - Retrieve URL For Action From Controller?

Dec 20, 2010

I am calling a Controller Action from a view, within that controller I need to invoke another Action which I will invoke to save the view to a network location as either HTML or Image.

How do I retrieve the URL to an Action from within a Controller. I need the actual URL, this means RedirectionToAction or View() wont work.

Why? I need to pass in a URL which will contain a call to a View. This view will be used to generate an image or HTML document using the System.Windows.Forms.WebBrowser.

.NET 3.5; C#; MVC 1;

I could do something like this, but its dirty ... well it leaves me with that dirty feeling.

using(Html.BeginForm("Action", "MyWorkflowController",
new {
MyId = "bla",
URLToGenerateImage = Url.Action("GenerateImage", "MyWorkflowController")
}))

View 2 Replies

MVC :: Same Processing For Each Action Of A Controller?

Nov 2, 2010

I would like all actions of a controller to read some master page level values from the querystring and write those values to ViewState. How do I do this in ASP.NET MVC?

View 1 Replies

MVC :: Getting Controller And Action From HttpContext?

Mar 8, 2010

What's the correct way to get the controller name and action from an httpContext object? I can see the values in the debug, but I don't seem to be able to get to them in the object code?

View 13 Replies

MVC :: Get Current Controller And Action?

Mar 10, 2010

I have a user control in a modal popup for login form. It's ajax post so when login is successfull it doesn't update the rest of the page. Would like to just redirect the user to thier current page but not 100% on how to do this from the accountController.

View 4 Replies

MVC :: Use JQuery To Get Controller Action?

Oct 19, 2010

I have this action on below

public ActionResult Edit(int id)
{
var mtn0120 = __context.GetMtn0120(id);

[code]...

View 4 Replies

MVC :: TDD Of A Controller.Action With An ActionFilterAttribute?

Jan 22, 2010

A well-known benefit of MVC is its suitablility for Test Driven Development (TDD) because you can directly call your controller actions from your test methods.

How can you test the combination of a controller action with a ActionFilter attribute (using OnActionExecuted to modify the ActionResult returned by the Action)? If I just call the Action, it returns the ActionResult from the action, but the filter attribute is never invoked. I think maybe you can get it by Controller.ActionInvoker.InvokeAction(controllerContext, "ActionName"), but you have to accurately mock so much of the controllerContext to make it work that it's a real pain.

View 2 Replies

Button That Performs A Server-side Action And Returns A File To Download?

Jul 28, 2010

I have an ASP.NET page (using ASP.NET AJAX and the Telerik RAD components) that provides a data table and an option to download data in CSV format.I have a requirement that a single button allow the user to download a data set. I have a system set up that's nearly working for me based off the solution in
http://stackoverflow.com/questions/104601/asp-net-response-redirect-to-new-window

<asp:Button ID="ExportCsvButton" runat="server" Text="Download to CSV/Excel"
byte[] csvBytes = someLongOperation();
Session[EXPORTED_SESSION_KEY] = csvBytes;
Response.Redirect("myexportpage.aspx", true);
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AppendHeader("content-disposition", "attachment; filename=" + "file.csv");
Response.ContentType = "text/csv";
Response.AddHeader("Pragma", "public");
Response.BinaryWrite(Session[OtherPage.EXPORTED_SESSION_KEY] as byte[]);
Response.Flush();
Response.End();
}


This actually works quite well. When I click the 'Export to CSV' button, the page's UpdateProgress kicks in and my loading overlay appears (it's a CSS div that covers the window when visible). This can take some time depending on the data set (potentially several minutes), at which point I'm getting a window popup with my file.

View 1 Replies

Can Map Control Event To Controller Action

May 12, 2010

I am creating my first MVC application.I have a View named "Product".I want to use two asp.net control MENU and MULTIVIEW and on OnMenuItemClick event of MENU I want to select the View of MULTIVIEW. Please tell me how can I process asp.net control event in a MVC application.Can we mapp control event to Controller Action.

View 1 Replies

MVC :: Wrong Action Invoked In Controller

Apr 8, 2010

Im fairly new to Asp.Net MVC. I spent some time to fix this but I get it to work. What I have is a:

QuestionController : Controller
{
public ActionResult Add()
{
//do stuff
}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Add(int roomuid)
{
//do stuff
}
}

now in my view i have : <a href = "/Question/Add">click here </a> The strange thing is that public ActionResult Add(int roomuid) is called and not Add() without get parameters. Even if I change the [AcceptVerbs(HttpVerbs.Get)] to [AcceptVerbs(HttpVerbs.Post)]

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved