【问题标题】:Error with generated view生成视图出错
【发布时间】:2014-01-20 11:42:29
【问题描述】:

以下代码有什么问题?它是由添加视图向导生成的。当我导航到控制器时,我收到以下错误: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; }
}

【问题讨论】:

  • 这是我的整个视图文件。我将添加控制器。

标签: c# asp.net asp.net-mvc asp.net-mvc-3 razor


【解决方案1】:

你的 id 是一个可以为空的类型,用这一行代替它:

  Network network = db.Networks.Find(id.Value);

还有这个:

 if (id == null)

可以改成这样:

 if (!id.HasValue)

问题出在以下行:

Network network = db.Networks.Find(id);

id 实际上是一个可以为空的类型,当它期望值时传递给您的 Find 方法。

对于可为空的类型,您可以使用 Value 属性读取基础值,即

id.Value

【讨论】:

  • 我的 ID 应该是 int 吗?不应该吗?我将我的模型添加到问题中。
  • 当然可以,但由于它是可空类型,因此您需要设置值而不是对象。我将更新问题以更好地解释。 :)
  • 好的,这非常适合我的细节操作。但是现在我在 Create 操作中收到了同样的错误。这是操作方法` public ActionResult Create() { return View(); }`
  • 好的,请问您能用其中的代码创建另一个问题吗?它更容易跟踪,在这里发布链接,我会看看。 :)
  • 另外,您能否在新问题中也发布用于创建的剃须刀,我想我需要看到。
猜你喜欢
  • 2019-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多