c# - 将一个模型从 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
解决方案
推荐阅读
- python - 从 python 帮助中提取示例到 ipython 会话中
- visual-studio - 在 VS19 中更改插入光标插入符号
- java - 错误:表达式的非法开始 void inputInfo();
- mysql - MySQL - 在一行中获取与外键相关的所有数据
- c++ - 尝试调用数组上的函数时抛出“数组类型'int [4]'不可分配”错误
- laravel - Laravel New Project_Name 命令在更新 laravel 6 后不起作用
- reactjs - 如何在带有 ui 材质的 onBlur 中使用 2 个函数?
- oracle - NO DATA FOUND oracle 使用游标
- node.js - 如何限制IP地址访问RabbitMQ
- vba - 将变量设置为 Property vba