How do I show the property that tag with "Hiddeninput"
I am working on my first MVC3 project. I am using linq to sql. I have one table in the database called "Tutorial". I use linq to sql designer generates dbml file. then create buddy class for valiation.
namespace Tut.DomainModel.Entities
{
[MetadataType(typeof(TutorialMetadata))]
public partial class Tutorial
{
public sealed class TutorialMetadata
{
[HiddenInput(DisplayValue = false)]
public int TutorialID { get; set; }
开发者_运维问答 [Required(ErrorMessage = "Title is required")]
[StringLength(250, MinimumLength = 10, ErrorMessage = "Title must be between 10 and 250 characters")]
public string Name { get; set; }
[HiddenInput(DisplayValue = false)]
public DateTime PostDate { get; set; }
}
}
}
In my TutorialsController.cs file, I have 2 actions. one is "List", another one is "Post".
public ViewResult List()
{
return View(reposi.Tutorials.ToList());
}
public ViewResult Post()
{
return View();
}
[HttpPost]
public ActionResult Post(Tutorial tutorial)
{
if (ModelState.IsValid)
{
reposi.Add(tutorial);
reposi.Save();
return RedirectToAction("List");
}
else
{
return View();
}
}
I labeled "PostDate" as hidden field because I dont need to show it when I create a new post. I just assign the current date to it.
but on my list page, I do want to show the post date, however its not showing because its labled at "HiddenInput" in the model class
how should I fix it?
By using a different view model for each view. So if in view1 you want a hidden field you design a view model for it and decorate the property with the HiddenInput attribute. And if view2 needs to show the value you design a different view model for it which doesn't have this attribute.
Conclusion: you should always create a view model per view. The view model is tightly coupled to the requirements of a given view and requirements change from view to view => different view models.
The worst mistake would be to try to reuse the same view model in different views. It leads to questions and problems like the one you are encountering currently.
精彩评论