-1

I'm new to MVC and trying to build an application which have departments and for each department there are employees listed under that department !

so my question is HOW do I set a default value Model.Count() in the View page

here's my code (Please note that when I loop in Model and if there's NO employee in that particular department the Model.Count() is always 0 and ActionLink is not appearing in the page ! so I need to set a default value for the Model, if you wondering why I'm using such implementation for the ActionLink that because I want to pass the current DepartmentId to the Create View and populate it to the @Html.HiddenFor for the employee Department automatically )

Any help would be appreciated

<p>
    @Model.Count() = 1; 

    @foreach (Employee employee in Model)
    {
        @Html.ActionLink("Create New", "Create", new { id = employee.DepartmentId })

        if (!employee.Equals(1))
        {
            break;
        }

    }    
</p>

My Controller

[HttpGet]
    [ActionName("Create")]
    public ActionResult Create_Get(int id)
    {
        Employee employee = new Employee();
        employee.DepartmentId = id; 
        return View(employee);
    }

    [HttpPost]
    [ActionName("Create")]
    public ActionResult Create_Post()
    {
        Employee employee = new Employee();
        TryUpdateModel(employee);

        if (ModelState.IsValid)
        {
            EmployeeContext employeeContext = new EmployeeContext();
            employeeContext.AddEmployee(employee);
            return RedirectToAction("Index","Employee", new { id = employee.DepartmentId });
        }
        return View();
    }

My Department and Employee list

  • Why are you assigning 1 to Model.Count() ? – derloopkat Oct 11 '16 at 00:42
  • @derloopkat it's not working it's just an example to show what I'm trying to achieve and regarding what I'm trying to do is elaborated above ! Thank you for your quick reply – Hussein ALSHAMIRI Oct 11 '16 at 00:56
  • Your loop does not make sense. Why would you want to generate a 'Create New' link that creates a new `Employee` for each existing employee (if you had 10 employees belonging to a department, you would be creating 10 identical links). –  Oct 11 '16 at 01:03
  • @StephenMuecke I know it doesn't make sense at all but I couldn't grab the employee.DepartmentId unless I did the above code and btw I just restricted the loop from creating 'Create new ' by using this code { if (!employee.Equals(1)) { break; }} – Hussein ALSHAMIRI Oct 11 '16 at 01:15
  • Your model is all wrong. You need a model with is a collection of `Department` (and a `Department` contains a collection of its `Employee`. Then in the view you loop each `Department` (and generate the link based on its ID) and for each department, use a nested loop to display its collection of `Employee` –  Oct 11 '16 at 01:18
  • @Hussein -Alhamari Are you trying to show a default maximum number of records to display in your view? Something like the "Show 10 entries" in http://jsfiddle.net/fauzi/UvjnT/ – Khyron Oct 11 '16 at 01:24
  • @StephenMuecke I've done this already but I couldn't pass the DepartmentId value to the Create View of the emloyee becuase I don't want the user to insert the DepartmentId himself I want to populate the field automatically Please review my question I've updated it with image – Hussein ALSHAMIRI Oct 11 '16 at 01:42
  • @Khyron I'm trying to Pass the DepartmentId value to the Create View of the employee to populate the DepartmentId field automatically – Hussein ALSHAMIRI Oct 11 '16 at 01:44
  • Its still not making sense. The image on the right shows employees for what? (all employees or employees for just a specif department - you don't even show any details of the department. You need to pas the department (with its employees to that view so you know what what the department is (and also display the department name so it makes some sense) –  Oct 11 '16 at 02:47

1 Answers1

0

Your code does not makes sense! Count() method returns the number of items in the collection. You cannot assign/set a new value to that! The left side part of an assignment statement should be a variable, property or indexer, Not a method!

What you should be ideally doing is, have a view model which has list of employees and department id.

public class EmployeeListVm
{
  public List<Employee> Employees { set;get;}
  public int DepartmentId { set;get;}
  public string DepartmentName { set;get; }
}

and in your GET action, set the 2 property values and send that to the view

public ActionResult EmployeeList(int departmentId)
{
   var vm =new EmployeeListVm();
   vm.DepartmentId=departmentId;

   //You can also get more info about department ex : Name
   var dept= GetDeapermentFromId(departmentId);
   vm.DepartmentName = dept.Name;

   // to do : Load vm.Employees
   return View(vm);
}

Assuming GetDeapermentFromId method accepts a department id and returns a department entity/DTO with data.

Now your view should be strongly typed to this new view model. Inside the view, now you can use the DepartmentId property as needed for building a link

@model EmployeeListVm
<h2>Showing employees for @Model.DepartmentName</h2>
@foreach(var item in Model.Employees)
{
  <p>@item.EmployeeName</p>
}
@Html.ActionLink("Create new","Create",new { id=Model.DepartmentId})
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Thank you for your solution I've implemented the same but when i'm trying to harvest the DepartmentId from the Model in the ActionLink the DepartmentId is not appearing in the intellisense and even if I writes as is it gives me an error stating that does not contains a definition for DepartmentId – Hussein ALSHAMIRI Oct 11 '16 at 01:50
  • @HusseinALSHAMIRI what is the model for your view ? – Shyju Oct 11 '16 at 02:19
  • @Khyron Please review the question again I just updated it with the Create controller and the Model name is **Employee** – Hussein ALSHAMIRI Oct 11 '16 at 02:43
  • Whew. This question has been quite the roller-coaster for me (wondering what the goal is, what the problem is). I haven't used `TryUpdateModel`, so I will have to bow out at this point. I would suggest having a read of http://stackoverflow.com/questions/7735635/real-example-of-tryupdatemodel-asp-net-mvc-3 It seems to suggest not using `TryUpdateModel` and instead recommends an approach I am more familiar with. That might not be a direct solution, but you said you were new to MVC so it might be informative. – Khyron Oct 11 '16 at 02:56
  • @Khyron if i try to get rid of the TryUpdateModel it gives me an error – Hussein ALSHAMIRI Oct 11 '16 at 13:01
  • 1
    @HusseinALSHAMIRI I suggest you read the answer thoroughly! I clearly mentioned in the answer that the view should be strongly typed to the new view model. – Shyju Oct 11 '16 at 13:33
  • @Khyron It's strongly typed already but the confusing part is that in the other Views the @Model.DepartmentId is working perfectly I figured out that if I get rid of the IEnumerable from `@model IEnumerable` the @Model.DepartmentId seems to work perfect but as you know I'm retrieving a collection of list Employees so I need to implement the IEnumerable ! so do you have any suggestions on who to overcome this issue – Hussein ALSHAMIRI Oct 11 '16 at 15:43
  • 1
    You can't just remove TryUpdateModel. It is the core of your entire approach to getting the data from the html response back in to your c# code. There are other ways to this that do not involve TryUpdateModel, such as the approach mentioned in the link I provided. Also please note you replied to the wrong person last time. I think Shyju has provided you with an answer. – Khyron Oct 11 '16 at 21:34