Abraham Josef
Abraham Josef

Reputation: 653

Map view Html controls to specific action object MVC

I have this action

public ActionResult Index(int id,int name, SomeObject object)
{
        //SomeCode
}

and the SomeObject class

public class SomeObject
{
public int Id {get; set;}
public int Name {get; set;}
}

my View

@using (Html.BeginForm())
{
@Html.TextBox("id", "")
@Html.TextBox("name", "")

@Html.TextBox("object_id", "")
@Html.TextBox("object_name", "")

<button class="btn-default" type="submit">Go</button>
}

every time I submit I got object.id and object.name parameters of index action have the same values of id and name, what can I do to get them correctly?

Note: I don't want to rename prameters

Upvotes: 0

Views: 47

Answers (2)

Jamie Rees
Jamie Rees

Reputation: 8183

Why can't you use @Html.TextBoxFor(x => x.id) (I am assuming your view had a model bound to it).

@using (Html.BeginForm())
{
@Html.TextBox("id", "")
@Html.TextBox("name", "")

@Html.TextBoxFor(x => x.Id)
@Html.TextBoxFor(x => x.Name)

<button class="btn-default" type="submit">Go</button>
}

Upvotes: 0

Edward N
Edward N

Reputation: 997

You should update your view look like this :

@using (Html.BeginForm())
{
@Html.TextBox("id", "")
@Html.TextBox("name", "")

@Html.TextBox("object.id", "")
@Html.TextBox("object.name", "")

<button class="btn-default" type="submit">Go</button>
}

Using "." instead of "_" and try another parameter name for "object" :)

Upvotes: 1

Related Questions