Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3213106
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:54:27+00:00 2026-05-17T14:54:27+00:00

I have a strongly typed view from which I want to insert some data.

  • 0

I have a strongly typed view from which I want to insert some data. However when I click on the submit button, the underlying data model is posted as a parameter to the controller. But when I view the contents of the parameter, it has not been updated with the user input.
Why is this?

The Controller looks like;

[HttpGet]
[Authorize(Roles = "Administrator, AdminAccounts")]
public ActionResult Add(int? id)
{
    ViewData["LastPerson"] = string.Empty;

    return View(new EmployeeViewModel()); 
}

[HttpPost]
[Authorize(Roles = "Administrator, AdminAccounts")]
public ActionResult Add(EmployeeViewModel employeeViewModel)
{
    if (ModelState.IsValid)
    {
        ViewData["LastPerson"] = employeeViewModel.Employee.Forename + " " +
                                employeeViewModel.Employee.Surname;
        employeeViewModel.Employee.Add();
    }
    return View(new EmployeeViewModel());
}

The View looks like;

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/AdminAccounts.master" 
Inherits="System.Web.Mvc.ViewPage<SHP.WebUI.Models.EmployeeViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Add
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="AdminAccountsContent" runat="server">

    <% using (Html.BeginForm("Add", "Employee")) {%>
        <%: Html.ValidationSummary(true) %>

        <fieldset>
            <legend>Add Employee</legend>
            <table>
                <tr>
                    <td align="right">
                <%: Html.LabelFor(model => model.Employee.Forename) %>
                   </td>                    
                    <td>
                <%: Html.EditorFor(model => model.Employee.Forename)%>
                <%: Html.ValidationMessageFor(model => model.Employee.Forename)%>
                    </td>
                </tr>
                <tr>
                    <td align="right">

                <%: Html.LabelFor(model => model.Employee.Surname)%>
                   </td>                    
                    <td>

                <%: Html.EditorFor(model => model.Employee.Surname)%>
                <%: Html.ValidationMessageFor(model => model.Employee.Surname)%>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                <%: Html.LabelFor(model => model.Employee.Middlenames)%>
                   </td>                    
                    <td>
                <%: Html.EditorFor(model => model.Employee.Middlenames)%>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                <%: Html.LabelFor(model => model.Employee.EmployeeNumber)%>
                   </td>                    
                    <td>
                <%: Html.EditorFor(model => model.Employee.EmployeeNumber)%>
                <%: Html.ValidationMessageFor(model => model.Employee.EmployeeNumber)%>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                <%: Html.LabelFor(model => model.Division.DivisionId) %>
                   </td>                    
                    <td>
                <%: Html.DropDownListFor(model => model.Division.DivisionId, Model.DivisionSelectList, "<--Select-->")%>
                <%: Html.ValidationMessageFor(model => model.Division.DivisionId)%>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                <%: Html.LabelFor(model => model.Department.DepartmentId) %>
                   </td>                    
                    <td>
                <%: Html.DropDownListFor(model => model.Department.DepartmentId, Model.DepartmentSelectList, "<--Select-->")%>
                <%: Html.ValidationMessageFor(model => model.Department.DepartmentId) %>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                <%: Html.LabelFor(model => model.Employee.AnnualLeaveAllocation) %>
                   </td>                    
                    <td>
                <%: Html.EditorFor(model => model.Employee.AnnualLeaveAllocation)%>
                <%: Html.ValidationMessageFor(model => model.Employee.AnnualLeaveAllocation)%>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                <%: Html.LabelFor(model => model.Employee.ChristmasAllocation)%>
                   </td>                    
                    <td>
                <%: Html.EditorFor(model => model.Employee.ChristmasAllocation)%>
                <%: Html.ValidationMessageFor(model => model.Employee.ChristmasAllocation)%>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                <%: Html.LabelFor(model => model.Employee.EmailAddress)%>
                   </td>                    
                    <td>
                <%: Html.EditorFor(model => model.Employee.EmailAddress)%>
                <%: Html.ValidationMessageFor(model => model.Employee.EmailAddress)%>
                    </td>
                </tr>
                <tr>
                    <td align="center" colspan="2" style="padding-top:20px;">
                    <input type="submit" value="Save" /></td>
                </tr>
            </table>
            <% if (ViewData["LastPerson"].ToString().Length > 0)
               { %>
                <p>
                   At time <% Response.Write(DateTime.Now.ToString("T")); %> 
                   - You have just entered <%Response.Write(ViewData["LastPerson"].ToString()); %>.
                </p>
             <%} %>
        </fieldset>

    <% } %>

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

The model looks like;

public class EmployeeViewModel
{
    public Employee Employee;
    public Department Department;
    public Division Division;
    public IList<Department> Departments { get; set; }
    public IEnumerable<SelectListItem> DepartmentSelectList
    {
        get
        {
            return this.Departments.Select(item => new SelectListItem
            {
                Text = item.DepartmentName,
                Value = item.DepartmentId.ToString()
            });
        }
    }
    public IList<Division> Divisions { get; set; }
    public IEnumerable<SelectListItem> DivisionSelectList
    {
        get
        {
            return this.Divisions.Select(item => new SelectListItem
            {
                Text = item.DivisionName,
                Value = item.DivisionId.ToString()
            });
        }
    }
    /// <summary>
    /// EmployeeViewModel() - CONSTRUCTOR
    ///                       Gets all of the divisions.
    ///                       Used to populate the drop down list so that you can assign 
    ///                       an employee to a division.
    /// </summary>
    public EmployeeViewModel()
    {
        this.Employee = new Employee();
        this.Departments = new List<Department>();
        this.Divisions = new List<Division>();
        using (SHPContainerEntities db = new SHPContainerEntities())
        {
            this.Divisions = db.Divisions.ToList();
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T14:54:27+00:00Added an answer on May 17, 2026 at 2:54 pm

    When you are posting to your add controller you are only passing in an Employee not an EmployeeViewModel. So you might want to try something like:

    [HttpPost]
    [Authorize(Roles = "Administrator, AdminAccounts")]
    public ActionResult Add(Employee employee)
    {
        var employeeViewModel = new EmployeeViewModel();
        if (ModelState.IsValid)
        {
            ViewData["LastPerson"] = employee.Forename + " " + employee.Surname;
            employeeViewModel.Employee = employee;
            employeeViewModel.Employee.Add();
        }
        return View(employeeViewModel);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a utility method which returns a strongly typed value from an old
I have a partial view which is not strongly typed. it is dynamic: <%@
I have a Search Edit view which is strongly typed to my Search model
i have a strongly typed asp.net-mvc view and the Model has a . Links
For strongly-typed & type-safe solution, I have to do the following step. Create some
I have a System.Web.Mvc.RazorView object which is strongly typed when in cshtml. Can I
Basically I have a set of checkboxes that are dynamically created from view data
I have a page which is strongly typed to my User class. When it
Let's say I have an edit view that is strongly-typed to a table called
i have problem to pass data from view to controller , i have view

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.