Just starting out in ASP.NET MVC - I have a pa开发者_开发技巧ge in ASP.NET MVC3 that has a searchbox, and when the user fills it out and searches, below the search box a webgrid shows the searchresults.
I have based my (razor) view on a model List<articles>
so the view knows what an article is and my webgrid can show me a list of articles. However, I also need the searchbox to be validated. I have a "searchmodel" that has the searchbox as a required field, but I can't base the view on both the List of articles as well as the "searchmodel" (can I?).
Ofcourse I would like the built-in (clientside and serverside) validation of MVC to work, but to do that I would need to base the view on my "searchmodel" and the gridview would no longer work.
Can anyone explain how I would go about this?
Thanks,
Erik
Define a view model:
public class MyViewModel
{
[Required]
public string Search { get; set; }
public List<articles> Articles { get; set; }
}
Now strongly type your view to MyViewModel
and have your controller action pass an instance of this new view model to the view instead of simply a List<articles>
. This way you have everything.
精彩评论