将视图 Html 控件映射到特定操作对象 MVC

Map view Html controls to specific action object MVC

我有这个动作

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

SomeObject class

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

我的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>
}

每次我提交时我都会得到 object.id and object.name 参数的 index 操作 具有相同的 id and name 值,我该怎么做才能正确获取它们?

注意:我不想重命名参数

您应该像这样更新您的视图:

@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>
}

使用“.”而不是“_”并尝试 "object" 的另一个参数名称 :)

为什么你不能使用 @Html.TextBoxFor(x => x.id)(我假设你的视图绑定了一个模型)。

@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>
}