2012-08-01 65 views
0

這是我的第一個MVC3項目,並認爲它會更簡單一些,但我遇到了很多問題。這似乎是我沒有遇到過解決方案的一個麻煩。作爲最後的手段在這裏發佈。mvc3模型使用列表 - 不存儲到數據庫

我的問題是,當創建一個新配方時,成分列表似乎永遠不會填充到數據庫中。我甚至試圖在保存其餘數據時對控制器中的列表進行硬編碼,但它仍然無能爲力。據我所知,在任何地方DB都沒有成分領域。任何人都可以指出我做錯了什麼?非常感謝。

型號:

public class Recipe 
{ 
    public int ID { get; set; } 

    [Required] 
    public string Title { get; set; } 

    [Required] 
    [Display(Name = "Type of Meal")] 
    public int TypeID { get; set; } 

    [Required] 
    public string Instructions { get; set; } 

    [Display(Name = "Submitted By")] 
    public string UserName { get; set; } 

    public IList<string> Ingredients { get; set; } 

    public virtual MealType Type { get; set; } 
} 

創建視圖:

@model final.Models.Recipe 

@{ 
    ViewBag.Title = "Recipe Finder - New Recipe"; 
} 

<h2>New Recipe</h2> 

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 
<script type="text/javascript"> 
    //adds an ingredient field on the fly 
    var numIngredients = 0; 
    function addIngredient() { 
     $('#ingredientlist').append('<tr><td><input type="text" name="Ingredients[' + numIngredients++ + ']" value="' + $('#AddIngredient').val() + '" readonly="true" tabindex="-1" /></tr></td>'); 

     $('#AddIngredient').val(''); 
     $('#AddIngredient').focus(); 
    } 

    function onKeyPress(e) { 
     var keycode; 

     if (window.event) { 
      keycode = window.event.keyCode; 
     } 
     else if (e) { 
      keycode = e.which; 
     } 
     else { 
      return true; 
     } 

     //for addingredient field, add ingredient and not submit 
     // else mimic submit 
     if (keycode == 13) { 
      if (document.activeElement.id == "AddIngredient") { 
       addIngredient(); 
       return false; 
      } 
      document.getElementById('btnSubmit').click(); 
     } 

     return true; 
    } 

    //intercepts form submit instead of using submit button to disable addingredient textbox 
    // this prevents the value/field from posting 
    function preSubmit() { 
     $('#AddIngredient').attr('disabled', 'true'); 
     document.getElementsByTagName('form')[0].submit(); 
    } 

    //intercepts users pressing the enter key 
    if (document.layers) document.captureEvents(Event.KEYPRESS); 
    document.onkeypress = onKeyPress; 
</script> 

@using (Html.BeginForm()) { 
    @Html.ValidationSummary(true) 
<fieldset> 
    <legend>by @User.Identity.Name</legend> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Title) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Title) 
     @Html.ValidationMessageFor(model => model.Title) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.TypeID, "Type") 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("TypeID", String.Empty) 
     @Html.ValidationMessageFor(model => model.TypeID) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Ingredients) 
    </div> 
    <div class="editor-field"> 
     @Html.TextBox("AddIngredient") 
     <input type="button" value="Add Ingredient" onclick="addIngredient()" tabindex="-1" /> 
     <table id="ingredientlist"> 
     </table> 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Instructions) 
    </div> 
    <div class="editor-field"> 
     @Html.TextAreaFor(model => model.Instructions, new { rows = "7", cols = "77" }) 
     @Html.ValidationMessageFor(model => model.Instructions) 
    </div> 

    @Html.HiddenFor(model => model.UserName) 

    <p> 
     <input type="button" value="Submit" onclick="preSubmit()" id="btnSubmit" /> 
    </p> 
</fieldset> 
} 

控制器:

[HttpPost] 
    public ActionResult Create(Recipe recipe) 
    { 

     if (ModelState.IsValid) 
     { 
      db.Recipes.Add(recipe); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     ViewBag.TypeID = new SelectList(db.MealTypes, "ID", "Type", recipe.TypeID); 
     return View(recipe); 
    } 

的POST領域,我可以看到成功發送的所有信息,如下圖所示,我我不知道問題是什麼,或者在這一點上我還沒有嘗試過。

Title:test recipe 
TypeID:2 
Ingredients[0]:test0 
Ingredients[1]:test1 
Ingredients[2]:test2 
Ingredients[3]:test3 
Instructions:this is a test 
UserName:tym 

回答

1

您可能要檢查this page有關存儲與實體相關的字符串列表。

+0

感謝您的幫助。我認真的在這哈哈上花了太多時間。 – 2012-08-01 17:02:59

相關問題