tags:

views:

132

answers:

3

Hi folks,

i have a simple form for an ASP.NET MVC application. I have a form property that is named differently (for whatever reason) to the real property name.

I know there's [Bind(Exlcude="", Include="")] attribute, but that doesn't help me in this case.

I also don't want to have a (FormsCollection formsCollection) argument in the Action method signature.

is there another way I can define the mapping?

eg.

<%= Html.ValidationMessage("GameServer", "*")%>

results in ..

<select id="GameServer" name="GameServer">
    <option value="2">PewPew</option>
</select>

this needs to map to..

myGameServer.GameServerId = 2; // PewPew.

cheers!

+1  A: 

i believe you will need to define it in your controller arguments or else it wouldnt have any clue what to accept.

public ActionResult GameServer(string GameServer){
    GServer myGameServer = new GServer();
    myGameServer.GameServerId.ToString() = GameServer;
    return View("GameServer");
}

you can pass in the name/id of the parameter your trying to go for on your view page, it will automagically know the value to recieve based on the id on your view.

Ayo
+1  A: 

What's wrong with having a FormCollection passed as argument? I had to do the same thing as you and I just excluded the GameServer property in the BindAttribute.

Another thing you have to note is that Html.ValidationMessage("GameServer", "*") won't work because the underlying model doesn't contain a GameServer property. You have to add it to the model. I don't know if there is a better way to do it, I didn't find but it was required to make ValidationMessage works

Davide Vosti
That ValidateMessage line is a mistake. i copied/pasted the wrong line. it should have been a textbox. I'll re-edit that when i get home. I'm thinking about using a forms collection .. but i was curious to see if it can be done WITHOUT it.
Pure.Krome
A: 

You can create you own ModelBinder (see how to) to do the custom mapping.
An overkill IMO, but you might have your reasons...

Eduardo Molteni