获取部分视图中元素的 id

发布于 2024-12-06 15:40:53 字数 1498 浏览 0 评论 0原文

我有一个观点,除其他外,包含一个元素列表。我想重用此列表,因此我将其移至部分视图中。我的视图包含一个表单,并且在发布时我想在列表中包含一些值。因此,我使用 @Html.EditorFor(m => m.Something) 生成输入框,该输入框将获得唯一的 ID。该视图是强类型的,使用“MyModel”作为模型。在后期,MVC 框架可以很好地处理所有事情 - 将值重新组合到包含输入和我使用 Html.HiddenFor 的一些字段的 MyModel 对象中。一切都好。

现在我将其移至部分视图。分部视图仅适用于列表,因此被强类型化为模型列表。我从主视图中包含此视图,并传入我希望它呈现的列表。分部视图内的表代码与之前完全相同,只是将其更改为从列表而不是 MyModel 中获取值。所有数据都显示得很完美,但是有一个问题。输入框不再获得 Id。而且因为它们没有获得 Id,所以它们的值不包含在发布的表单中 - 因此它们不会出现在我作为 post 方法的输入获得的模型中。我尝试使用 @Html.Partial("_MyPartial", Model.Items)@{Html.RenderPartial("_MyPartial", Model.Items);}

所以问题是;我怎样才能在部分视图下完成这项工作?我怎样才能找回那个ID?

这是一些示例代码,以防有任何不清楚的地方:

MyModel.cs
----------
public class MyClass 
{ 
    public string Val1 { get; set; }
    public string Val2 { get; set; }
}

public class MyModel
{
    public List<MyClass> Items {get; set; } 
}

MyView.cshtml: 
--------------
@model MyModel
using (Html.BeginForm("SomeAction", "SomeController", FormMethod.Post))
{
    @Html.Partial("_MyPartial", Model.Items)
}

_MyPartial.cshtml: 
------------------
@model List<MyClass>

<table>
    <tbody>
    for (var i = 0; i< Model.Count; i++)
    {
        <tr>
            <td>
                @Model[i].Val1
                @Html.HiddenFor(model => model[i].Val1)
            </td>
            <td>@Html.EditorFor(model => model[i].Val2)</td>
        </tr>
    }
    </tbody>
</table>

I have a view that - among other things - contains a list of elements. I want to reuse this list, so I move it out to a partial view. My view contains a form, and on post I want to include some values in the list. Therefore I use @Html.EditorFor(m => m.Something) to generate input-boxes which will get a unique Id. The view is strongly typed to use "MyModel" as Model. On post the MVC framework handles everything very well - putting the values back together to a MyModel object containing the input and some fields I used Html.HiddenFor on. All good.

Now I move this to a partial view. The partial view only works on the list and therefore is strongly typed to model List. I include this view from my main view, and pass in the list I want it to render. The table code inside the partial view is exactly what it was before except that it is changed to get values from the list instead of MyModel. All data is displayed perfectly, but there is a problem. The input-boxes doesn't get an Id any more. And because they don't get an Id their value isn't included in the posted form - and hence they don't appear in the model I get as input to my post method. I tried including the partial view using both @Html.Partial("_MyPartial", Model.Items) and @{Html.RenderPartial("_MyPartial", Model.Items);}

So the question is; how can I make this work with a partial view? How can I get that Id back?

Here is some sample code in case anything is unclear:

MyModel.cs
----------
public class MyClass 
{ 
    public string Val1 { get; set; }
    public string Val2 { get; set; }
}

public class MyModel
{
    public List<MyClass> Items {get; set; } 
}

MyView.cshtml: 
--------------
@model MyModel
using (Html.BeginForm("SomeAction", "SomeController", FormMethod.Post))
{
    @Html.Partial("_MyPartial", Model.Items)
}

_MyPartial.cshtml: 
------------------
@model List<MyClass>

<table>
    <tbody>
    for (var i = 0; i< Model.Count; i++)
    {
        <tr>
            <td>
                @Model[i].Val1
                @Html.HiddenFor(model => model[i].Val1)
            </td>
            <td>@Html.EditorFor(model => model[i].Val2)</td>
        </tr>
    }
    </tbody>
</table>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一紙繁鸢 2024-12-13 15:40:53

您还必须为您的 Model.Items 使用 EditorFor。
因此,将 _MyPartial 更改为更相关的内容,例如 MyEditorForMyClass.cshtml
在共享文件夹或您正在使用的文件夹中创建一个“EditorTemplates”文件夹,并将“MyEditorForMyClass.cshtml”文件放入其中。

然后执行以下操作:

MyView.cshtml: 

--------------
@model MyModel
using (Html.BeginForm("SomeAction", "SomeController", FormMethod.Post)
{
    @Html.EditorFor(model => Model.items,"MyEditorForMyClass"}

}

EditorFor 模板/帮助程序方法允许您呈现标准数据类型和您自己的包含多个属性的复杂对象。

You will have to use EditorFor for your Model.Items as well.
So change the _MyPartial to something more relevant, like MyEditorForMyClass.cshtml.
Make a 'EditorTemplates' folder in the Shared or the folder you're working in and place the 'MyEditorForMyClass.cshtml' file in there.

Then do the following:

MyView.cshtml: 

--------------
@model MyModel
using (Html.BeginForm("SomeAction", "SomeController", FormMethod.Post)
{
    @Html.EditorFor(model => Model.items,"MyEditorForMyClass"}

}

The EditorFor templates/helper methods allows you to render both standard data-types and your own complex objects that contain multiple properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文