Link for code samples used in the demo
http://csharp-video-tutorials.blogspo...
Link for csharp, asp.net, ado.net, dotnet basics, mvc and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat...
In this video we will discuss, preventing unintended updates in mvc. Please watch Part 19, before proceeding.
Modify "Edit" controller action method that is decorated with [HttpPost] attribute as shown below. This method is present in "EmployeeController.cs" file.
[HttpPost]
[ActionName("Edit")]
public ActionResult Edit_Post(int id)
{ EmployeeBusinessLayer employeeBusinessLayer = new EmployeeBusinessLayer(); Employee employee = employeeBusinessLayer.Employees.Single(x =] x.ID == id); UpdateModel(employee, new string[] { "ID", "Gender", "City", "DateOfBirth" }); if (ModelState.IsValid) { employeeBusinessLayer.SaveEmployee(employee); return RedirectToAction("Index"); } return View(employee);
}
Please note:
1. The name of the method is changed from "Edit" to "Edit_Post"
2. The method is decorated with [ActionName("Edit")] and [HttpPost] attributes. This indicates that, this method is going to respond to "Edit" action, when the form is posted to the server.
3. The "id" of the employee that is being edited, is passed as a parameter to this method.
4. Using the "id" parameter we load the employee details(Id, Name, Gender, City & DateOfBirth) from the database.
Employee employee = employeeBusinessLayer.Employees.Single(x =] x.ID == id);
5. We then call UpdateModel() function. This should automatically update "Employee" object with data from the posted form. We are also passing a string array as the second parameter. This parameter specifies the list of model properties to update. This is also called as include list or white list. Notice that, we did not include "Name" property in the list. This means, even if the posted form data contains value for "Name" property, it will not be used to update the "Name" property of the "Employee" object.
UpdateModel(employee, new string[] { "ID", "Gender", "City", "DateOfBirth" });
So, if we were to generate a post request using fiddler as we did in the previous session, "Name" property of the "Employee" object will not be updated.
Alternatively, to exclude properties from binding, we can specify the exclude list as shown below.
[HttpPost]
[ActionName("Edit")]
public ActionResult Edit_Post(int id)
{ EmployeeBusinessLayer employeeBusinessLayer = new EmployeeBusinessLayer(); Employee employee = employeeBusinessLayer.Employees.Single(x =] x.ID == id); UpdateModel(employee, null, null, new string[] { "Name" }); if (ModelState.IsValid) { employeeBusinessLayer.SaveEmployee(employee); return RedirectToAction("Index"); } return View(employee);
}
Notice that we are using a different overloaded version of UpdateModel() function. We are passing "NULL" for "prefix" and "includeProperties" parameters.
UpdateModel[TModel](TModel model, string prefix, string[] includeProperties, string[] excludeProperties)
asp.net core docker Part 20 Preventing unintended updates in mvc | |
| 329 Likes | 329 Dislikes |
| 139,211 views views | 524K followers |
| Education | Upload TimePublished on 21 May 2013 |
Related keywords
wcf vs web api,including but not limited to,fieldset bootstrap,including definition,ado.net core,sql server management studio,webkinz,webadvisor,craigslist nj,properties.settings.default,wcf one piece,asp.net core 3,ajax players,fields tradução,weber grill parts,webtoon,tutorials by hugo,csharp corner,mvc design pattern,asp.net machine account,servers for minecraft,asp.net core dependency injection,ado.net tutorial,model t,model s,including other term,services angular,ajax ontario,controller spring,properties of materials,asp.net mvc tutorial,fields of verdun sabaton,properties are doubled while in a breach,asp.net cos'è,csharp assembly,tutorialspoint python,sql join,services briefcase,asp.net core web api,data entry,ajax jquery,wccftech,craigslist ny,data warehouse,including meaning,fields of verdun,asp.net zero,including me or myself,csharp foreach,server memes,including or included,fields medal,sql date format,services online,chase,controller pioneer,model cars,controller php,serverless architecture,server resume,data science,wcf c#,properties file java,server books,tutorialspoint javascript,mvcc connect,ado.net mysql,services technologies gps,data scientist,including that of,controller pak n64,ajax deadpool,server jobs,modeling agencies,model x,cvs,website,model y,mvc tutorial,costco hours,wcf service application,controlleradvice,controller angularjs,controller ableton,tutorialspoint spring,serverless,wcf soap,contrôleur dj,properties c#,wcf cat,fields floripa,wcf test client,cool math games,model o mouse,services & training hse colombia sas,servicestack,citibank,asp.net core identity,sql union,ajax parking,sql database,asp.net core logging,properties python,mvconnect,asp.net guida,cunyfirst,wcf nba,csharp download,wcfi foundation,csharp online,wcf authentication,model s tesla,tutorials near me,including comma,http://asp.net,server 2019,properties scotland,ado.net descargar,web of dreams,chernobyl,serverminer,including example,ajax cleaner,model t ford,ado.net visual studio 2019,including me,properties datei,webassign,ado.net vs entity framework,data star trek,data lake,ado.net visual studio 2017,csharp list,fields of gold cifra,sql like,asp.net mvc,asp.net core tutorial,sqlite,wcfm,ajax roster,mvc architecture,including me in tagalog,http://ado.net,modelo negra,asp.net core mvc,ajax soccer,server hosting,data scientist salary,controller ps4,fields of gold sting,fields of verdun lyrics,wcfi,ajax dish soap,controller ps4 pc,capital one,fields of green,server rack,tutorialspoint html,data mining,csharp interface,craigslist,webroot,tutorialspoint reactjs,ajax request,wcf dragon ball,asp.net core 2.2,tutorialspoint python 3,sql developer,webster,services transmission company sas,sql group by,fields of history the great war,asp.net core signalr,services manager,mvc framework,ajax paving,mvc near me,data analysis,properties deutsch,fields of gold tradução,tutorialspoint spring boot,including you in tagalog,properties synonym,mvc map,csharp online compiler,including meaning in tagalog,fields area measure,asp.net download,sql between,modeling clay,data analyst salary,ado.net c# pdf,wcf 2019 nba,services tag dell,csharp switch,ado.net ventajas y desventajas,csharpstar,wcf tutorial,including everything,modeling,tutorialspoint,fields of greens,controller for all,controller pro switch,ajax meaning,including in the loop,csharp-video-tutorials.blogspot,tutorials dojo,csharp string format,central park 5,ado.net c#,asp.net core github,controller chaos,server status,ajax fc,server jobs nyc,asp.net core swagger,sql formatter,credit karma,services group,server error in '/' application,properties dict,data entry jobs,services windows,asp.net core 3.0,http://controller.access.network,sql injection,wcf ria services,including in a sentence,tutorialspoint c#,calculator,ado.net entity data model,sql insert,data analytics,tutorialspoint tableau,services google play apk,model engine kits,properties ireland,sqlyog,asp.net core 3 release date,sql server,server job description,tutorials by a,properties-maven-plugin,servicenow,webcam,mvc hours,webmd symptom,controllership,csharp array,properties mallorca,csharp enum,ajax call,asp.net core 2. guida completa per lo sviluppatore,asp.net core,server pro,controller firmware revision 2.10.00 embedded media manager failed media attach,server status ffxiv,model x tesla,fieldset html,webcrims,cheap flights,properties datei öffnen,asp.net core hosting,including you in the loop,services sas,model a ford,properties to yaml,modells,tutorialspoint java,tutorialspoint java compiler,datadog,webmd,csharp to json,college football,ado.net dataset,csharp dictionary,cnn,website builder,tutorialspoint sql,asp.net web api,server side rendering,weber grills,sql server 2017,mvc nj,tutorialspoint spark,data breach,ado.net oracle,properties java,asp.net core download,csharp to vb.net,webster bank,webstaurant,datacamp,tutorialsystems,ajax post,services fms publish announcement,services.msc no abre,modelo abv,fieldset,ajax jersey,csharp operator,asp.net core razor pages,server duties,asp.net core environment variables,csharp random,fieldset css,century 21,controller midi,services consultores,services consulting,mvcsd,mvcsc,services.msc,ado.net pdf,asp.net core configuration,ajax marvel,properties übersetzung,including synonyms,sql update,asp.net tutorial,mvc medical,ado.net entity data model visual studio 2019,wcfs international curriculum,mvc2,ado.net entity data model visual studio 2017,controller switch,chase online,fields of gold,wcf api,costco,server jobs near me,webex,sql meaning,sql tutorial,fields of the nephilim,properties matlab,including in tagalog,sql commands,including abbreviation,controller area network,data universe,ado.net entity framework,ajax tavern,controller symfony,tutorialsteacher,ajax javascript,database,services desk,properties file,data visualization,model 3,ajax greek,csharp tutorial,mvc pattern,ado.net sql server,ado.net connection,asp.net identity,mvcu,databricks,modelo beer,asp.net core middleware,data analyst,modelo especial,wcf web service,mvc webadvisor,web store,mvcc,webmail,mvci,mvctc,fields automação,
Không có nhận xét nào:
Đăng nhận xét