首页 > 解决方案 > 将一个模型从 ViewModel 发送回控制器

问题描述

我正在使用 ViewModel 将各种模型传递给视图。现在在一个表单中,我想从这个 ViewModel 返回一个模型(ResetTypeSingleModel),但是控制器的方法只能期望 ViewModel:我的 ViewModel:

public class ResetViewModel {
    public IEnumerable<ResetTypeModel> TableResetType {get; set;}

    public ResetTypeSingleModel SingleResetType {get; set;}

    }

我的控制器(当然我把它改成了 ResetTypeSingleModel):

public IActionResult ResetDevice([FromForm] ResetViewModel rtm){ }

我的观点:

@using (Html.BeginForm("ResetDevice", "Sccm", FormMethod.Post, new { id = "form_reset" }))
{  
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    @Html.DropDownListFor(model=> model.SingleResetType.resettype_id, new SelectList(Model.TableResetType, "resettype_id" , "resettype_name"), "Please select",  new { @CssClass="ddl_resettype", @id="ddl_resettype", @class = "form-control" })

   <input type="submit" value="Reset" class="btn btn-primary" />

}

我怎样才能实现我只提交这个模型而不是完整的 viewModel 给控制器?

谢谢斯蒂芬


更新:所以我尝试更改字段的名称,但模式等不再起作用:

@{ string name = @Html.NameFor(model => Model.SingleResetType.hostname).ToString().Split(".").Reverse().ToList()[0];}       
        @Html.LabelFor(model => model.SingleResetType.hostname)
        @Html.TextBox(name, null, new { @class = "form-control", @id=name, name =name, })

所以我想唯一的方法是使用javascript

标签: c#asp.net-coreasp.net-core-mvc

解决方案


推荐阅读