0

Iam new in mvc i want to create performance object using create page in mvc and i have models called "Performance" ,"Performer" and "Performerperformance" i want to create performance with multiple performer so i create new custom model and use it in create view but when i change data in page and click save button model is return to create function with null value i don't know what is the problem

create View :

@model eltazkara_admin.ViewModels.CustomPerformancePerformersModel

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

        <div class="form-group">
            @Html.LabelFor(model => model.performanceObj.EventId, "Event", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("EventId", (IEnumerable<SelectListItem>)ViewBag.EventDropDownList, htmlAttributes: new { @class = "form-control select2" })
                @Html.ValidationMessageFor(model => model.performanceObj.EventId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.performanceObj.VenueId, "Venue", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("VenueId", (IEnumerable<SelectListItem>)ViewBag.EventDropDownList, htmlAttributes: new { @class = "form-control select2" })
                @Html.ValidationMessageFor(model => model.performanceObj.VenueId, "", new { @class = "text-danger" })
            </div>
        </div>

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


        <div class="form-group">              
                @Html.EditorFor(model => model.performanceObj.StartDate, new { htmlAttributes = new { @class = "form-control", id = "startDate", type = "hidden" } })
                @Html.EditorFor(model => model.performanceObj.EndDate, new { htmlAttributes = new { @class = "form-control", id = "endDate", type = "hidden" } })
            </div>

        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.performanceObj.Views, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.performanceObj.Views, new { htmlAttributes = new { @class = "form-control", @Value = "0" } })
                @Html.ValidationMessageFor(model => model.performanceObj.Views, "", new { @class = "text-danger" })
            </div>
        </div>

        @using (Html.BeginForm("AddPerformerToNewPerformance", "Performances"))
        {

            <div class="form-group">
                @Html.LabelFor(model => model.AvailablePerformers, "Performers", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <br />

                    <select name="SelectedPerformer" id="SelectedPerformer">
                        @foreach (var item in Model.AvailablePerformers)
                        {
                            <option value="@item.Value">@item.Text</option>
                        }

                    </select>
                    <input type="submit" value="Add Performer" class="btn btn-default" />

                </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>
    <h4>
        @Html.ActionLink("Back to List", "Index")
    </h4>
</div>

create function in controller :

  public ActionResult Create()
    {
        CustomPerformancePerformersModel cppm = new CustomPerformancePerformersModel();
        var availablePerformers = db.Performers.Where(x => x.Deleted != true).ToList();
        cppm.AvailablePerformers = availablePerformers.Select(x => new SelectListItem
        {
            Text = x.Name,
            Value = x.PerformerId.ToString()
        }).ToList();


        ViewBag.EventId = new SelectList(db.Events, "EventId", "Name");
        ViewBag.VenueId = new SelectList(db.Venues, "VenueId", "Name");
        ViewBag.PerformerId = new SelectList(db.Performers, "PerformerId", "Name");

    }


    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(CustomPerformancePerformersModel customperformance)
    {
        if (ModelState.IsValid)
        {
            customperformance.performanceObj.Score = customperformance.performanceObj.Views;
            db.Performances.Add(customperformance.performanceObj);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.EventId = new SelectList(db.Events, "EventId", "Name", customperformance.performanceObj.EventId);
        ViewBag.VenueId = new SelectList(db.Venues, "VenueId", "Name", customperformance.performanceObj.VenueId);
        ViewBag.Performers = new SelectList(db.PerformerPerformances, "PerformersId", "Name", customperformance.performanceObj.PerformerPerformances);
        return View(customperformance);

    }

and the customviewmodel :

  using eltazkara_admin.Models;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
namespace eltazkara_admin.ViewModels
 {
     public class CustomPerformancePerformersModel
    {
        public Performance performanceObj;
        public List<SelectListItem> AvailablePerformers { get; set; }
        public string selectedPerformer { get; set; }

      }
    }
amal mansour
  • 345
  • 2
  • 5
  • 14
  • 1
    For a start you have nested forms which is invalid html and not supported –  Feb 16 '17 at 22:11
  • but i make it in edit page and it work well @StephenMuecke – amal mansour Feb 16 '17 at 22:15
  • Did you nit read my comment - nested forms are invalid - your inner form will never work! –  Feb 16 '17 at 22:17
  • even if i remove this form it didn't work , i try to remove this form but still model returned with null value – amal mansour Feb 16 '17 at 22:21
  • Because `performanceObj` is a field, not a property and the `DefaultModelBInder` does not bind fields - add `{get; set; }`. But this is awful practice. A view model should not contain a data model. Your view model contains properties of `Performance` (plus `IEnumerable` properties for you dropdownlists and get rid of all those `ViewBag` properties) - [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Feb 16 '17 at 22:26

0 Answers0