1

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!

2
  • <%= Html.ValidationMessage("GameServer", "*")%> ? Is this example correct??? Commented Mar 24, 2009 at 14:30
  • Nope - i'll fix that later on when i get home. it should have been a text box. was a bad copty/paste error. Commented Mar 25, 2009 at 23:55

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

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.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.