C# MVC can't retrieve data from view to controller

558 views c#
5

I am new to ASP.Net and I am sure this is a very basic question. I have a employee crud views . In create view i was created viewModel to pass different two model . One Model display emloyee and another model connect to db and retrieve all departments to my view . But I can't retrieve data to my employee create controller area. Vs send this err Abc.Models.MyViewModel.Employee.get returned null. On Debug.Print(employee.Employee.Name) line.

Here my Models ;

Employee.cs

 public class Employee
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Surname { get; set; }
    [Required]
    public string phoneNumber { get; set; }
    public string Detail { get; set; }

    //Bu k?s?mda veri taban? ili?kisi 1 to many olacak
    public int DepartmentId { get; set; }
    public Department Department { get; set; }
}

Department.cs

public class Department
{
    public int Id { get; set; }
    public string depName { get; set; }

    public List<Employee> Employees { get; set; }
}

MyViewModel.cs

    public class MyViewModel
{
    public Employee Employee { get; set; }
    public IEnumerable<Department> departments { get; set; }
}

I added dropboxlist on my create.cshtml area with MyViewModel and i can't retrieve all data on my controller .

Here my EmployeeController.cs ;

// GET: Employee/Create
    [HttpGet]
    public ActionResult Create()
    {

        MyViewModel viewModel = new MyViewModel();
        Employee emp = new Employee();
        viewModel.Employee = emp;
        viewModel.departments = db.Departments;

        return View(viewModel);
    }

    // POST: Employee/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 ActionResult Create(/*[Bind(Include = "Id,Name,Surname,phoneNumber,Department,Detail")]*/ MyViewModel employee)
    {
        //string emName = employee.Employee.Name;
        Debug.Print(employee.Employee.Name);
        if (ModelState.IsValid)
        {
            db.Employees.Add(employee.Employee);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(employee);
    }

Here Create.cshtml "this html codes are in @using (Html.BeginForm()");

@model TelefonRehberi.Models.MyViewModel

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

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

    <div class="form-group">
        @Html.LabelFor(model => model.Employee.phoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Employee.phoneNumber, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Employee.phoneNumber, "", new { @class = "text-danger" })
        </div>
    </div>
    <!--Dropdown List Olacak-->
    <div class="form-group">
        @Html.LabelFor(model => model.Employee.Department, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <!--Burada zorland?m umar?m do?ru kullan?m olmu?tur.-->
            @Html.DropDownListFor(m => m.Employee.Department, new SelectList(Model.departments.Select(i => i.depName)), " - Select or Add -", new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Employee.Department, "", new { @class = "text-danger" })
        </div>

    </div>
    <!--Dropdown List Sonu Ayarlamalar Yap?lacak.-->
    <div class="form-group">
        @Html.LabelFor(model => model.Employee.Detail, htmlAttributes: new { @class =

answered question

1 Answer

7

Add Form and try to do

@using (Html.BeginForm("ActionName","ControllerName", FormMethod.Post))
{
    <div class="form-horizontal">
        <!--your code here -->
        <!--your code here -->
    </div>
}

posted this

Have an answer?

JD

Please login first before posting an answer.