0

I have ModelView which I have created for purpose to create new record instance via view-- razor form. In controller I need to assign list data to ViewModel; IEnumerable and then pass this model along with with list data (of CategoryType) which then in view I need to populate in drop-down list, followed by ID of selected CategoryType send back to controller along with other data.

I have assign IEnumerable value to model in controller but not sure is correct and how to do rest part in view ???

View Model - CompanyProfileModelView

public class CompanyProfileModelView
{
    public Company _Company { get; set; }

    public IEnumerable<CategoryType> _CategoryType { get; set; } 
}

Model Class

public class CategoryType
{
    public int CategoryTypeID { get; set; }
    public string CategoryTitle { get; set; }

    public ICollection<Company> Companies { get; set; }
}

Controller Class

 [HttpGet]
    public ActionResult CreateCompany()
    {
        var _listData = _appFunctions.GetAllCategory();

        var _model = new CompanyProfileModelView
        {
            _CategoryType = _listData
            ??????????????
        };

        return PartialView("_CreateNewCompanyPartial", _model);
    }

    [HttpPost]
    public ActionResult CreateCompany(CompanyProfileModelView _model)
    {
        try
        {
            if (ModelState.IsValid)
            {
                //my code will be here to read for input data
            }
        }
        catch (DataException ex)
        {
            ModelState.AddModelError("", "Unable To Create New Function Navigation" + ex);
        }

        return RedirectToAction("Home");
    }

View

@model App.DAL.Model.CompanyProfileModelView

 @using (Html.BeginForm("CreateCompany", "CompanyProfile", FormMethod.Post, new { id = "NewFunctionNavigationForm" }))
{
 <div class="form-group">
        @Html.LabelFor(@model => @model._Company.CompanyName, new { @class = "control-label col-md-2" })
        <div class="form-group">
            @Html.EditorFor(@model =>@model._Company.CompanyName)
            @Html.ValidationMessageFor(@model=>@model._Company.CompanyName)
        </div>
    </div>

    <div class="form-group">          
        // need help here for dropdown of CategoryType list  
    </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>
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
K.Z
  • 5,201
  • 25
  • 104
  • 240
  • Please take a look [here](http://stackoverflow.com/questions/20242981/asp-net-mvc-dropdown-list-from-selectlist). You may get an idea of what need to be done. – Siva Gopal Feb 09 '15 at 13:18
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). BTW, it's "Thanks in advance", not "Thanks in advanced". – John Saunders Feb 10 '15 at 03:34

1 Answers1

2

Since DropDownList uses IEnumerable<SelectListItem>, change the view model to

public class CompanyProfileModelView
{
  public Company Company { get; set; }
  public SelectList CategoryList { get; set; } 
}

and assuming Company model contains

[Display(Name="Category")]
public int? CategoryType { get; set; }

Controller

[HttpGet]
public ActionResult CreateCompany()
{
    var listData = _appFunctions.GetAllCategory();
    var model = new CompanyProfileModelView
    {
      CategoryList = new SelectList(listData, "CategoryTypeID ", "CategoryTitle")
    };
    return View(model);
}

[HttpPost]
public ActionResult CreateCompany(CompanyProfileModelView model)
{
  if (!ModelState.IsValid)
  {
    // Re-assign select list if returning the view
    var listData = _appFunctions.GetAllCategory();
    model.CategoryList = new SelectList(listData, "CategoryTypeID ", "CategoryTitle");
    return View(model)
  }
  // Save and redirect
}

View

@model App.DAL.Model.CompanyProfileModelView
@using (Html.BeginForm()) // Note, no parameters required in this case
{
  ....
  @Html.LabelFor(m => m.Company.CategoryType, new { @class = "control-label col-md-2" })
  @Html.DropDownListFor(m => m.Company.CategoryType, Model.CategoryList, "--Please select--")
  @Html.ValidationMessageFor(m => m.Company.CategoryType)
  .....
  <input type="submit" value="Create" class="btn btn-default" />
}