Part 43 Hiddeninput and readonly attributes in mvc

kudvenkat
kudvenkat
93.3 هزار بار بازدید - 11 سال پیش - HiddenInput attribute is useful when
HiddenInput attribute is useful when you want to render a property using input type=hidden. This attribute is extremely useful, when you don't want the user to see or edit the property, but you need to post the property value to the server when the form is submitted, so the correct record can be updated. HiddenInput is present in System.Web.Mvc namespace.

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
@aarvikitchen5572

ReadOnly attribute is present in System.ComponentModel namespace. As the name suggests, this attribute is used to make a property readonly. Please note that, we will still be able to change the property value on the view, but once we post the form the model binder will respect the readonly attribute and will not move the value to the property.  You can also, make property of a class readonly simply, by removing the SET accessor.

Changes to Employee.cs file used in the demo. Notice that Id property is decorated with HiddenInput attribute, and EmailAddress is decorated with ReadOnly attribute.
public class EmployeeMetadata
{
   // Id property is hidden and cannot be changed
   [HiddenInput(DisplayValue=false)]
   public int Id { get; set; }

   // EmailAddress is read only
   [ReadOnly(true)]
   [DataType(DataType.EmailAddress)]
   public string EmailAddress { get; set; }
       
   [ScaffoldColumn(true)]
   [DataType(DataType.Currency)]
   public int? Salary { get; set; }

   [DataType(DataType.Url)]
   [UIHint("OpenInNewWindow")]
   public string PersonalWebSite { get; set; }
       
   [DisplayAttribute(Name= "Full Name")]
   public string FullName { get; set; }

   [DisplayFormat(DataFormatString="{0:d}")]
   public DateTime? HireDate { get; set; }

   [DisplayFormat(NullDisplayText="Gender not specified")]
   public string Gender { get; set; }
}

Changes to HomeController.cs file.
public ActionResult Edit(int id)
{
   SampleDBContext db = new SampleDBContext();
   Employee employee = db.Employees.Single(x =] x.Id == id);
           
   return View(employee);
}

[HttpPost]
public ActionResult Edit(Employee employee)
{
   if (ModelState.IsValid)
   {
       SampleDBContext db = new SampleDBContext();
       Employee employeeFromDB = db.Employees.Single(x =] x.Id == employee.Id);

       // Populate all the properties except EmailAddrees
       employeeFromDB.FullName = employee.FullName;
       employeeFromDB.Gender = employee.Gender;
       employeeFromDB.Age = employee.Age;
       employeeFromDB.HireDate = employee.HireDate;
       employeeFromDB.Salary = employee.Salary;
       employeeFromDB.PersonalWebSite = employee.PersonalWebSite;

       db.ObjectStateManager.ChangeObjectState(employeeFromDB, System.Data.EntityState.Modified);
       db.SaveChanges();
       return RedirectToAction("Details", new { id = employee.Id });
   }
   return View(employee);
}

Text version of the video
http://csharp-video-tutorials.blogspo...

Slides
http://csharp-video-tutorials.blogspo...

All ASP .NET MVC Text Articles
http://csharp-video-tutorials.blogspo...

All ASP .NET MVC Slides
http://csharp-video-tutorials.blogspo...

All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenka...

All Dot Net and SQL Server Tutorials in Arabic
kudvenkatarabic
11 سال پیش در تاریخ 1392/04/04 منتشر شده است.
93,380 بـار بازدید شده
... بیشتر