2014-01-20 81 views
1

以下代碼有什麼錯誤?它是由添加視圖嚮導生成的。當我瀏覽到控制器我收到以下錯誤: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; } 
} 
+1

潛在價值。我將添加控制器。 – Zapnologica

回答

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 
+0

我的ID應該是一個int?不應該嗎?我在模型中加入了我的模型。 – Zapnologica

+0

當然,但是因爲它是可以爲空的類型,所以需要設置值而不是對象。我會更新這個問題來解釋更好一點。 :) – hutchonoid

+0

好吧,我的細節行動完美的工作。但是現在我在創建操作中收到同樣的錯誤。這裏是操作方法'public ActionResult Create() { return View(); }' – Zapnologica