部分视图,MVC应用程序

时间:2018-01-30 17:37:23

标签: asp.net-mvc web

我有一个应用程序,使用了visual studio MVC web app的很多自动生成的代码。

我有3个控制器,每个控制器都能很好地完成工作。

通常,自动代码为每个特定视图执行1页,但我决定将其中一些用作部分视图:

我的具体问题是:

控制器1:负责处理食谱的创建。每个配方都可以有很多成分,ingridients的控制器是控制器2。

    import numpy as np
    list1=np.random.randint(low=50,high=100,size=50).reshape(10,5)
    for i in list1.flat:
        print(i)
    list2=np.array(i,dtype=int)
    list2

创建视图,错误发生的地方

    public ActionResult Create()
    {
        return View();
    }

    // POST: Recipes/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create([Bind(Include = "RecipeId,Name,Instructions")] Recipe recipe)
    {
        if (ModelState.IsValid)
        {
            db.Recipes.Add(recipe);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        return View(recipe);
    }

前3个代码行,完美呈现食谱的索引视图(listview),以便一个正常工作。

然而,接下来的2行,渲染视图,创建配方的ingridients,但是当我按下创建它失败。应该注意的是,如果我直接打开ingridient创建视图,则按预期创建属于recipy的ingridient。

控制器2负责处理ingridients的创建:

@using System.Data.Entity.Core.Metadata.Edm
@using IwantFood.Models
@model IwantFood.Models.Recipe

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Create</title>
</head>
<body>
 @Scripts.Render("~/bundles/jquery")
 @Scripts.Render("~/bundles/jqueryval")


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Recipe</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Instructions, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Instructions, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Instructions, "", new { @class = "text-danger" })
            </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>
}

    @{
        var db = new DataContext();
        var recipelist = db.Recipes.AsEnumerable();
        Html.RenderPartial("Index", recipelist);

        ViewBag.RecipeId = new SelectList(db.Recipes, "RecipeId", "Name", db.IngredientRecipes);
        //Html.RenderPartial("Create", "IngredientRecipes", new RouteValueDictionary());
        //Html.RenderPartial(Url.Content("~/Views/IngredientRecipes/Create.cshtml"));
    }

创造完整的成分(全部)

public ActionResult Create()
    {
        ViewBag.RecipeId = new SelectList(db.Recipes, "RecipeId", "Name");
        return View();
    }

    // POST: IngredientRecipes/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create([Bind(Include = "IngredientId,RecipeId,IngredientName")] IngredientRecipe ingredientRecipe)
    {
        if (ModelState.IsValid)
        {
            db.IngredientRecipes.Add(ingredientRecipe);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        ViewBag.RecipeId = new SelectList(db.Recipes, "RecipeId", "Name", ingredientRecipe.RecipeId);
        return View(ingredientRecipe);
    }

问题似乎是父子关系和局部视图,问题,但我不知道如何解决它:/ 非常感谢你提前!

0 个答案:

没有答案