0

这是我的第一个 MVC3 项目,我认为完成它会更简单一些,但我遇到了很多问题。这似乎是我没有遇到解决方案的一个麻烦。在这里张贴作为最后的手段。

我的问题是,当创建新配方时,成分列表似乎永远不会填充到数据库中。当保存其余数据时,我什至尝试在控制器中硬编码一个列表,但它仍然没有做任何事情。据我所知,数据库中的任何地方都没有成分字段。谁能指出我做错了什么?非常感谢。

模型:

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
4

1 回答 1

1

您可能需要查看有关存储与实体关联的字符串列表的此页面。

于 2012-08-01T16:55:24.363 回答