Zapnologica
Zapnologica

Reputation: 22556

Error with generated view

What is wrong with the following code? It was generated by teh add view wizard. And when I navigate to the controller I get the following error: String was not recognised as a valid Boolean and the following line is highlighted: @using (Html.BeginForm())

Here is the view:

  @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")
}`

Controller:

//
    // 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);
    }

Domain Model:

   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; }
}

Upvotes: 1

Views: 59

Answers (1)

hutchonoid
hutchonoid

Reputation: 33305

Your id is a nullable type, replace it with this line instead:

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

And this:

 if (id == null)

Can be changed to this:

 if (!id.HasValue)

The problem will be with the following line:

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

The id is actually a nullable type that is passed to your Find method when it is expecting the value.

With nullable types you can read the underlying value with the Value property i.e.

id.Value

Upvotes: 1

Related Questions