Part 17 Editing a model in mvc

42
2



Text version of the video

Slides

All ASP .NET MVC Text Articles

All ASP .NET MVC Slides

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

In this video we will dsicuss editing a model in mvc. Please watch Part 16, before proceeding.

Step 1: Copy and paste the following “Edit” controller action method in “EmployeeController.cs” file.
[HttpGet]
public ActionResult Edit(int id)
{
EmployeeBusinessLayer employeeBusinessLayer = new EmployeeBusinessLayer();
Employee employee = employeeBusinessLayer.Employees.Single(emp =] emp.ID == id);

return View(employee);
}

Please note:
1. This method is decorated with [HttpGet] attribute. So this method only responds to HTTP get request when editing data.
2. The “Edit” action method also receives “id” of the employee that is being edited. This “id” is used to retrieve the employee details.
3. The employee object is passed to the view

Step 2: Add “Edit” view
a) Right click on the “Edit” controller action method, and select “Add view” from the context menu
b) Set
View name = Edit
View engine = Razor
Select “Create a strongly-typed view” check box
Model class = “Employee”
Scaffold template = “Edit”
Finally click “Add” button
c) This should add “Edit.cshtml” to “Employee” folder in “Views” foolder
d) Delete the following scripts section that is present at the bottom of “Edit.cshtml” view
@section Scripts
{
@Scripts.Render(“~/bundles/jqueryval”)
}

Run the application and navigate to This page should list all the employees. Click on “Edit” link. The “Edit” page should display the details of the “Employee” being edited. Notice that, by default “textboxes” are used for editing. It is ideal to have a dropdownlist for gender rather than a textbox. To achieve this. make the following changes to “Edit.cshtml”

REPLACE THE FOLLOWING CODE
@Html.EditorFor(model =] model.Gender)
@Html.ValidationMessageFor(model =] model.Gender)

WITH
@Html.DropDownList(“Gender”, new List[SelectListItem]
{
new SelectListItem { Text = “Male”, Value=”Male” },
new SelectListItem { Text = “Female”, Value=”Female” }
}, “Select Gender”)
@Html.ValidationMessageFor(model =] model.Gender)

Run the application. Edit an employee, and notice that a DropDownList is used for gender as expected. Post the form by clicking on “Save” button. We will get an error stating – The resource cannot be found. We will discuss fixing this in our next video.

Nguồn: https://wijstaanvooronzegrondrechten.org

Xem thêm bài viết khác: https://wijstaanvooronzegrondrechten.org/cong-nghe

42 COMMENTS

  1. i can't understand how the dropdownlist is automaticatically filled with selected option?Plz reply if someone knows

  2. Hi. Why I can't name my int variable in Edit method like I want ? For example I would like to name it as 'employeeId' instead of 'id'. I get an error if I change it to whatever I want. Thanks

  3. Hello Sir,
    I am big fan of your videos, your teaching technique, practical demos are really awesome, now I am learning MVC from your videos, And facing one issue in the same part 17 Editing a model in MVC. The Problem is when I am clicking Edit Button, it shows complete data in view and as suggested for drop-down control I made necessary changes, but its selecting optional value that is "Select Gender". Which should be selected value, like male or female. Why this is happening, I did same things that you are doing, can you please help me with that. If possible please let me know 🙂 Thank you for your wonderful videos 🙂

  4. hi i was facing issue. The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for —
    In URL i have http://localhost:50189/Employee/Edit/1001 but in action it is null.

  5. hi sir, i have a problem. When I click on the edit, the dropdownlist does not show the default Male or Female. I tried set selected = true which also not solved, can you hellp me ?

    I checked the value to pass to the view . And the previous value func @ htmlDropDownList and then is not the same, my code
    ================@@=================
    code view :
    <div class="col-md-10">
    @{((List<SelectListItem>)ViewBag.lstem)[0].Selected=true;}
    @{Response.Write(((List<SelectListItem>)ViewBag.lstem)[0].Selected);}
    @{Response.Write(((List<SelectListItem>)ViewBag.lstem)[1].Selected);}

    @Html.DropDownList("Gender", (List<SelectListItem>)ViewBag.lstem, "Select Gender", htmlAttributes: new { @class = "control-label col-md-2" })

    @{Response.Write(((List<SelectListItem>)ViewBag.lstem)[0].Selected);}
    @{Response.Write(((List<SelectListItem>)ViewBag.lstem)[1].Selected);}
    @*@Html.EditorFor(model => model.Gender, new { htmlAttributes = new { @class = "form-control" } })*@

    @Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
    </div>
    code controller:
    public ActionResult Edit(int id)
    {
    EmployeeBusinessLayer employeeBusinessLayer = new EmployeeBusinessLayer();
    Employee employee = employeeBusinessLayer.Employees.Single(em => em.id == id);

    List < SelectListItem > lst =new List<SelectListItem>
    {
    new SelectListItem() { Text = "Male", Value ="Male",Selected =true },
    new SelectListItem() { Text = "Female", Value ="Female"}
    };
    ViewBag.lstem = lst;

    return View(employee);
    }

  6. Hi Venkat sir, while implementing the 'Edit' functionality parameter 'id' in EDIT() is coming as null.Can you please help me.

  7. Ha, I had the infamous gender dropdown not selected problem because I had set the gender field in the db as nchar(10) so there were extra spaces after the gender text. Need to be careful with this stuff….!

  8. Hi venkat Sir,
    my employee table consist of department id of a particular employee on edit employee details department dropdownlist is not getting bind,as i am storing id of the department in employee table..
    =================================================MY Code===========================
    <div class="editor-field">
    @Html.DropDownList("Department", new List<SelectListItem>
    {
    new SelectListItem{Text="IT" ,Value="1"},
    new SelectListItem{Text="Accounts" ,Value="2"},
    new SelectListItem{Text="Finance" ,Value="3"},
    },"select Department")
    @Html.ValidationMessageFor(model => model.DeptId)
    </div>

  9. i have a problem that during click the edit option my edit view take all the data from Db except dropdown they still remain on select gender option please help me out

  10. Hello Sir, I can't retrieve my ID field from SQL db. All other fields are displayed perfectly, but without ID I can't click on edit.
    When I hover on all Action links, it shows ID as 0. Please help.

  11. I love your video's, they are very helpful! I never found a better way to learn than your video series!!!

    But please keep in mind there are more genders than only male and female, I would really appreciate it if you would add 'Other' to the dropdownlist of gender.

  12. Hello Sir, i am following the videos from starting.
    At this point in this tutorial, I am getting error with the edit button click.
    When i click on the Edit button it is not populating the url with the ID.
    Can you please help me.

  13. I want to implement async page, when I click button I get result(by code previously entered in editbox) and display it in the same page. also a questio can I connect the get method via andorid java code?

  14. Sir I have a question: when we write 
    —– employeeBusinessLayer.Employees.Single(emp => emp.ID == id) —–
    does it mean that we will 1) retrieve the list of all the employees and 2) take the one with the correct id. So will it be slower than finding in the database the employee with that id and return only that employee ?

  15. Sir Your tutorial is great source to learn from basic to Advance.

    I have one doubt in whole lecture you never used Model folder of MVC rather you had all in Business layer. But can you upload some lecture about Model use and how can we separate all SQLs from Model in MVC.

  16. Sir I have a problem at editing mode at the time of clicking an edit link it populates the url like this localhost:49310/StudentList/Edit it wouldn't display any no. appended with the url and also i have mention the key for primary key field

  17. Hi Venkat ,
    Could you please help me ,i run this code and click EDIT link i've got an error like this:
    The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32)' in 'insert.Controllers.EmployeeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

    Could u pls help me how can i get rid of this…..

  18. hi venkat,
    could u tell us how to increase the width of dropdown list pls.in a web application v use width="35px" for ex.here how do v do…pls tell us..

  19. object to the view say Edit.cshtml, all the data corresponding to the Employee ID will be bounded to the controls present in the view. Please check the code above.
    Is there anything wrong, please correct me Venkat Sir.

  20. Hi,
    The Get method of ‘Edit’ action goes as

    public ActionResult Edit(int id)
    {
    EmployeeBusinessLayer employeeBusinessLayer =
    new EmployeeBusinessLayer();
    Employee employee =
    employeeBusinessLayer.Employees.Single(emp => emp.ID == id);

    return View(employee);
    }
    Since we fetch the data of the Employee record under Edit, using its employee ID and assign it to the employee object and passing that object to
    /*2 b continued*/

  21. hi,

    ASP.NET MVC uses a complete Model-View-Controller architecture that combines the Controller and the View in a way that both meet the dependency of each other.

    -The testing of the architecture can be done by instantiating a View and carrying out the unit tests without running the controllers through the complete cycle.

  22. Dear Sir,
    Greetings!
    I follow your videos regularly and thanks for your helping hand. I am new to .net technologies and learning MVC with your videos. I have a basic question which was asked in one of the interview. What is the life cycle of ASP.NET MVC page cycle as like ASP.NET page cycle. I know you dont have much time to spend on comments and if any one who reads this please reply and will be helpful for others too – Thanks, Raju

  23. Hi Venkat,
    How the hell, does the edit mask preselect Mary's selectbox with female, WITHOUT giving this informations to the List[SelectListItems] list … ?

    If there is no additional information, it usually has to show 'Select Gender' … ?!?

    Could you please explain it? 🙂

LEAVE A REPLY

Please enter your comment!
Please enter your name here