c# - ASP.Net MVC 发送新的对象来查看用坏数据填充一些字段
问题描述
昨天我问了一个问题,关于如何解决突然和意外的“对象未设置为对象的实例”错误。我得到的唯一真正答案是将对象的新实例传递给视图,如下所示:
// GET: WC_Inbox/Create
public ActionResult Create(int? id)
{
System.Diagnostics.Debug.WriteLine("Employee ID was: " + id);
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Employee employee = db.Employees.Find(id);
if (employee == null)
{
return HttpNotFound();
}
string fullName = employee.First_Name + " " + employee.Last_Name;
System.Diagnostics.Debug.WriteLine("Employee full name: " + fullName);
ViewBag.EmployeeID = id;
ViewBag.Name = fullName;
ViewBag.Status = "Pending";
return View(new WC_Inbox());
}
有问题的主要线路在这里:return View(new WC_Inbox());
这行得通,但它产生了进一步的问题。现在在我的创建页面上,我在某些字段中得到了一些不正确的数据
Org Number、Hire Date、Work Schedule 和 Injury date 字段不应显示这些值。这些是不正确的,并且对于应用程序所针对的大多数老年客户来说会非常困惑。我希望占位符值显示在那里,而不是这个不正确的数据。
这是这些字段的 Create 视图中的代码:
<div class="form-group">
@Html.LabelFor(model => model.Org_Number, "Org Number", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Org_Number, new { htmlAttributes = new { @class = "form-control", placeholder = "0025" } })
@Html.ValidationMessageFor(model => model.Org_Number, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Hire_Date, "Hire Date", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Hire_Date, new { htmlAttributes = new { @class = "form-control", placeholder = "8/3/2021" } })
@Html.ValidationMessageFor(model => model.Hire_Date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Job_Title, "Job Title", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Job_Title, new { htmlAttributes = new { @class = "form-control", placeholder = "Programmer Analyst" } })
@Html.ValidationMessageFor(model => model.Job_Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Work_Schedule, "Work Schedule", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Work_Schedule, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Work_Schedule, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Injury_Date, "Injury Date", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Injury_Date, new { htmlAttributes = new { @class = "form-control", placeholder = "8/14/2021" } })
@Html.ValidationMessageFor(model => model.Injury_Date, "", new { @class = "text-danger" })
</div>
</div>
我怎样才能摆脱那些坏数据并使用我分配的占位符值呢?
PS:工作时间表应该是空白的。没有占位符值。
解决方案
问题是您将默认对象传递给强类型视图。在线上:
return View(new WC_Inbox());
您还没有做任何事情来初始化WC_Inbox
对象的属性/值。这意味着所有值都将是默认值(0 代表int
,null
代表string
,最小日期时间DateTime
等)。这是您在视图中看到的那些默认值。如果您想要空白默认值,您只需更新WC_Inbox
类以支持可为空的类型。这是有效的,因为null
值显示为空白,因此您的占位符文本将显示出来。例如,它按预期工作,Job_Title
因为Job_Title
is astring
并且它的默认值null
是然后显示为EditorFor
允许占位符文本显示的空白。希望这是有道理的——它很简单,但解释起来很冗长。
如果您的WC_Inbox
类可以允许为空值,请更新它,然后您的占位符将起作用。所以这个类现在看起来像:
class WC_Inbox
{
public int? Org_Number {get; set;}
public DateTime? Hire_Date { get; set;}
public string Job_Title {get; set;} // string is nullable already
public DateTime? Work_Schedule {get; set;}
public DateTime? Injury_Date {get; set;}
}
如果您更新定义,WC_Inbox
那么您拥有的代码将起作用。
推荐阅读
- c# - C# 如何将一组整数变量转换为字节数组?
- c++ - Gradle - Visual C++ 编译器、vcpkg 和外部库
- java - HTL data-sly-use 的用法?
- python-3.x - 熊猫数据框组/块的标准化(如何快速插入行)
- python - 尝试挂载卷时出现python kubernetes API错误
- angular - 使用 Graph QL 放大如何查询相关对象
- excel - 在 Excel 中对具有不均匀数据集的两列进行排序和匹配
- angular - 触发observer.error产生控制台错误
- sql-server - 生成没有文件组的脚本
- flutter - Flutter 中有没有办法让两个用户共享一个屏幕?