views:

82

answers:

2

I have a really odd issue with ASP.Net MVC.

I have a form, which is posting 3 text values to an action method called create user (for simplicity sake, say it is the following);

public ActionResult CreateUser(string FirstName, string LastName, string Email)
    {
        var s = FirstName + LastName + Email;

        return RedirectToAction("Index");
    }

Say also my form is

    <% using (Html.BeginForm("CreateUser", "User"))
   { %>

    <%=Html.TextBox(("FirstName")) %>
    <%=Html.TextBox(("LastName")) %>
    <%=Html.TextBox(("Email")) %>

    <div><input type="submit" value="Submit" /></div>

<% } %>

Now in my action method, on the user controller, the values FirstName, LastName and Email are all null!

However, if I copy the same method to another controller (Game), and update the form to post there, the values in the method are not null! I am totally stumped with this one.

Both Controllers are the same - they inherit the same base class, have the same attributes applied to them etc.

EDIT: Got it working (not sure what the underlying issue was).

I had a custom attribute on my Index method on the User controller (this essentially parsed the HttpContext.Current.User.Identity.Name property and automatically passed it into the method). For some reason, this was problematic on the Index method, once I removed it everything started working as intended!

This was my Index method before:

[Authorisation]
public Action Index(string userName){...}

and after

public Action Index() {...}
A: 

Try add a breakpoint inside CreateUser and take a look at the content of Request.Form (or QueryString), or ValueProvider. The keys might be different.

Ikhwan
The ValueProvider, in User, has 3 values (controller, action, id), with values "Users", "Index" and "" respectively (I find this odd, given that it should be the "Users", "CreateUser" action.When I switch it to post to the game controller, I get 4 values in the ValueProvider ("FirstName", "controller", "action" and "id") with values "Jamie", "Game", "CreateUser" and ""), and it works as expected.I cannot figure out why the ValueProvider is different simply by changing controllers? I tried renaming the controller from User to Users but the results are the same....
Jamie
+1  A: 

By the sounds of things (works when you change the controller it posts to) it'll probably have something to do with 'User' being a reserved word of some type.

It kinda makes sense, User is used in the asp.net framework to refer to the current IPrincipal object.

So yeah, rename your controller to something different.

HTHs, Charles

Charlino