gpt4 book ai didi

asp.net-mvc-4 - .NET MVC 4 强类型 ViewModel 包含强类型模型,且 EditorFor 和 EditorTemplate 部分 View 未绑定(bind)

转载 作者:行者123 更新时间:2023-12-02 02:26:39 25 4
gpt4 key购买 nike

关于此问题有很多问题...但不知何故,我无法使此绑定(bind)正常工作,并且我仍然在发布的 View 模型中收到空值。这是 MVC 4。

这是主视图模型

public class RoleVM {

[Required]
[Display(Name = "Name of the Role")]
public string Role {get; set;}

public IEnumerable<RolePermission> permissions { get; set; }

}

这是 RolePermission 类

public class RolePermission {

public int id;

public bool permission_value;

public string name { get; set; }

}

这是 Controller 中的 GET 创建方法

public ActionResult Create() {

RoleVM role_vm = new RoleVM();

var allpermissions = from p
in permission_repo.GetPermissions()
select p;

role_vm.permissions = from p
in allpermissions
select new RolePermission
{ name = p.name, id = p.PermissionId, permission_value = false };

return View(role_vm);
}

这是 Create.cshtml 文件

@model RoleVM
@using (Html.BeginForm("Create", "Role",
FormMethod.Post, new { @class = "permission_form" }))
{
@Html.ValidationSummary(true)

<fieldset>
<legend>RoleVM</legend>

<div class="form-item">
@Html.LabelFor(model => model.Role)

@Html.EditorFor(model => model.Role)
@Html.ValidationMessageFor(model => model.Role)
</div>

@Html.EditorFor(model => model.permissions)

<p>
<input class="submit-btn" type="submit" value="Create" />
</p>
</fieldset>
}

接下来是位于 ~/Views/Shared/EditorTemplates 中的 rolepermissions.cshtml 文件

@model RolePermission

<div class="form-item">
@Html.HiddenFor(modelItem => modelItem.id)
@Html.LabelFor(modelItem => modelItem.permission_value, Model.name)
@Html.CheckBoxFor(modelItem => modelItem.permission_value)
</div>

这是在页面上呈现的 html 项目之一的示例

<div class="form-item">

<input data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." id="permissions_2__id" name="permissions[2].id" type="hidden" value="3" />

<label for="permissions_2__permission_value">Role-Edit</label>
<input data-val="true" data-val-required="The Boolean field is required." id="permissions_2__permission_value" name="permissions[2].permission_value"
type="checkbox" value="true" /><input name="permissions[2].permission_value" type="hidden" value="false" />

</div>

最后是创建 POST 方法

    [HttpPost]
public ActionResult Create(RoleVM rolevm)
{

//In here rolevm.role is populated based on the textbox input
//However rolevm.permissions is there with the correct
//number of items, but the values all are not binded
// id is null, name is empty, and permission_value is false
// regardless of which checkboxes were checked


return RedirectToAction("Index");
}

有关已发布模型的绑定(bind)问题的任何帮助都将非常有用。

最佳答案

这是一个简单的问题,如果其他人遇到这个问题,也许这会对他们有所帮助。我没有使用 get 声明为属性的类属性;放;

public class RolePermission {

public int id { get; set; }

public bool permission_value { get; set; }

public string name { get; set; }

}

关于asp.net-mvc-4 - .NET MVC 4 强类型 ViewModel 包含强类型模型,且 EditorFor 和 EditorTemplate 部分 View 未绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11487197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com