gpt4 book ai didi

c# - .net mvc View 不在发布时返回模型

转载 作者:太空宇宙 更新时间:2023-11-03 15:22:10 25 4
gpt4 key购买 nike

在我开始之前我想说我搜索过并没有发现类似的东西
在我的解决方案中,我有一个模型,其中包含我的一些对象的列表

public class ModelView
{
public Owner owner = new Owner();
public Tenant tnt = new Tenant();
}

在我看来,我把这个类称为模型,就是这样

@model WebApp.Models.ModelView

<form name="export_form" action="Export" method="post">
<table cellpadding="2" cellspacing="2" border="0">
@if (Condition_1)
{
<tr>
<td>
<!-- ID -->
</td>
<td>
@Html.HiddenFor(model => model.owner.ID)
</td>
</tr>
<tr>
<td>
Name
</td>
<td>
@Html.EditorFor(model => model.owner.name)
</td>
</tr>
<tr>
<td>
Phone
</td>
<td>
@Html.CheckBoxFor(model => model.owner.is_Checked_Phone)
</td>
</tr>
}
else
{
<tr>
<td>
<!-- ID -->
</td>
<td>
@Html.HiddenFor(model => model.tnt.ID)
</td>
</tr>
<tr>
<td>
Name
</td>
<td>
@Html.EditorFor(model => model.tnt.name)
</td>
</tr>
<tr>
<td>
Adress
</td>
<td>
@Html.CheckBoxFor(model => model.tnt.is_Checked_Adress)
</td>
</tr>
}
</table>
<input type="submit" name="SaveStuff" value="Save" />
<input type="submit" name="ExportStuff" value="Export" />
</form>

在我的 Controller 中,我有一个处理多个提交按钮的类,根据按钮名称,它会重定向到一个方法。下面是 SaveStuff 方法

    [HttpPost]
[SubmitButtonClass(Name = "SaveStuff")]
public ActionResult Save_Definition(Owner owner, Tenant tnt)
{
/*
Stuff Here
*/
}

这里的问题是我一直在获取空值,即使实体不为空。有什么原因吗?没有返回任何值。

更新
模型A

 public partial class Owner
{
public long ID { get; set; }
public bool is_Checked_Name { get; set; }
public bool is_Checked_Phone { get; set; }
}

模型 B

 public partial class Tenant
{
public long ID{ get; set; }
public bool is_Checked_Name { get; set; }
public bool is_Checked_Adress { get; set; }
}

这些是使用 EF 自动生成的

最佳答案

您的代码存在多个问题。首先你认为的模型是ModelView并且您生成以该模型属性名称为前缀的表单控件,例如

<input type="checkbox" name="owner.is_Checked_Phone" ... />

这意味着您的 POST 方法需要匹配 View 中的模型

public ActionResult Save_Definition(ModelView model)

下一个问题是您的模型只有字段,没有 { get; set; } 的属性, 所以 DefaultModelBinder不能设置任何值。您的 View 模型需要诸如 public Owner owner { get; set; } 之类的属性然后在将模型传递给 View 之前在 Controller 中设置值,或者在 ModelView 的无参数构造函数中设置值.

然而, View 模型不应该包含数据模型的属性,而应该是一个只包含您需要的属性的平面结构。在你的情况下,它将是

public class ModelView
{
public long ID { get; set; }
[Display(Name = "Name")]
public bool IsNameSelected { get; set; }
[Display(Name = "Phone")]
public bool IsPhoneSelected { get; set; }
[Display(Name = "Address")]
public bool IsAddressSelected { get; set; }
// additional property to define if the form is for an Owner or Tenant
public bool IsOwner { get; set; }
}

在 GET 方法中

var model = new ModelView()
{
IsOwner = true // or false
};
return View(model);

在 View 中

@model ModelView
....
@using (Html.BeginForm("Save_Definition"))
{
@Html.HiddenFor(m => m.ID)
@Html.HiddenFor(m => m.IsOwner)
@Html.CheckBoxFor(m => m.IsNameSelected )
@Html.LabelFor(m => m.IsNameSelected )
if (Model.IsOwner)
{
@Html.CheckBoxFor(m => m.IsPhoneSelected)
@Html.LabelFor(m => m.IsPhoneSelected)
}
else
{
@Html.CheckBoxFor(m => m.IsAddressSelected)
@Html.LabelFor(m => m.IsAddressSelected)
}
.... // submit buttons
}

还有POST方法,可以查看model.IsOwner的值知道您是否提交了 OwnerTenant并采取适当的行动。

旁注:

  1. 推荐您阅读What is ViewModel inMVC?
  2. <table>元素用于表格数据。不要将其用于布局。
  3. 您的 View 有 <form action="Export" .. >但是您的 POST 方法是名为 Save_Definition所以不确定你打算用哪种方法将表格提交给。

关于c# - .net mvc View 不在发布时返回模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36806187/

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