以下代码有什么问题?它是由添加视图向导生成的。当我导航到控制器时,我收到以下错误:String was not recognised as a valid Boolean
并突出显示以下行:@using(Html.BeginForm())
以下是观点:
@model Radio_Management_Interface.DomainModel.Entities.Network
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout_main.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Network</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.name)
@Html.ValidationMessageFor(model => model.name)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.startCode, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.startCode)
@Html.ValidationMessageFor(model => model.startCode)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.frequency, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.frequency)
@Html.ValidationMessageFor(model => model.frequency)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}`
控制器:
//
// GET: /Networks/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Network network = db.Networks.Find(id);
if (network == null)
{
return HttpNotFound();
}
return View(network);
}
域名模型:
public class Network
{
public int networkID { get; set; }
public string name { get; set; }
public int startCode { get; set; }
public decimal frequency { get; set; }
public virtual List<Block> blocks { get; set; }
}
答案 0 :(得分:1)
您的ID是可以为空的类型,请将其替换为此行:
Network network = db.Networks.Find(id.Value);
而且:
if (id == null)
可以改为:
if (!id.HasValue)
问题出在以下几行:
Network network = db.Networks.Find(id);
id实际上是一个可以为null的类型,当它期望值时会传递给Find方法。
使用可空类型,您可以使用Value属性读取基础值,即
id.Value