mardi 30 janvier 2018

Partial view, MVC app

I got a application, using alot of the auto generated code by the visual studio MVC web app.

I got 3 controllers, which individually each do their job well.

Normally the auto code does 1 page for each specific view, but i decided to use some of them as partial view:

My specific problem is this:

Controller 1: Responsible for handling the creation of Recipes. Each recipe can have many ingredients, controller for ingridients is controller 2.

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

Create View, Where the mistake is made

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

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

The first 3 code lines, perfectly render the Index view (listview) of the recipes, so that one is working fine.

however, the next 2 line, render the view, of the creation of ingridients for a recipe, but when i press create it fails. It should be noted that if i open the ingridient creation view directly, the ingridient belonging to a recipy is created as intended.

Controller 2 Responsible for handling the creation of ingridients:

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

Creation of ingredients full (full)

@model IwantFood.Models.IngredientRecipe

<!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>IngredientRecipe</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.RecipeId, "RecipeId", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("RecipeId", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.RecipeId, "", new { @class = "text-danger" })
            </div>
        </div>

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

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

The problems seems to be a parent child relation and partial view, problem but i dont know how to fix it :/ Thank you very much in advance!




Aucun commentaire:

Enregistrer un commentaire