开发者

MVC generic search and model validatation issue

开发者 https://www.devze.com 2023-03-10 23:14 出处:网络
I\'ll explain a generic scenario. To explain my problem, I\'d request some patience until I explain how I\'ve implemented my Search page. I\'ve a [Users] table and an [Address] table based on which I\

I'll explain a generic scenario. To explain my problem, I'd request some patience until I explain how I've implemented my Search page. I've a [Users] table and an [Address] table based on which I've derived a VIEW in SQL - [vw_User_Addr]. I'm using L2S so that I'll get a strongly typed vw_User_Addr class.

Now I've a search page so apparently I'll derive it from this class like:

<%开发者_如何学运维@ Page Title="User Search" ... Inherits="System.Web.Mvc.ViewPage<MyDAL.vw_User_Addr>" %>

My List action returns a List which I use in the View to form the Grid table. Everything is fine till here. Now I want to implement search on this page. So this is what I found -

  • In Controller during the List Action I create ViewData["Usr"] = new vw_User_Addr();
  • In my View I do:

    <% MyDAL.vw_User_Addr usr = ((MyDAL.vw_User_Addr)(ViewData["Usr"])); %>

  • Then I use this "usr" object to bind my search control like:

    <%= Html.TextBox("FirstName", usr.FirstName)%>

  • Finally in my HttpPost handler action I object vw_User_Addr like:

    [HttpPost] public ActionResult List(vw_User_Addr searchObj){...}

  • I use this searchObj to extract the values user might have populated in the search controls and then I perform search.


So, I hope I explained well. This is how I do my search in MVC2. Here're my concerns/questions:

  1. When I click on the search image-button I get a postback but it fires a ModelValidation which gives error. Not with all but atleast those which are int (i.e. like Roles dropdown search control) - how to handle that?

  2. After a lot of R&D I've settled that I've to either do a ModelState.Clear(); or a more complex way to handle this. Is there a better option?

  3. Is there a better way to achieve the search-implementation explained above? Am I using the standard\best way to do search in MVC?

  4. At core-level, can I make L2S understand that this is an SQL-View so its Readonly and all the fields can be NULL - so that the Mr.DefaultModelValidator doesn't perform such illogical validations?

  5. If 4. is viable can I set the properties of all the fields in the L2S view to be readonly to give it a try?

Thank you for your precious time and review. Pls guide me if I'm not on the track. I believe this is the simplest search-scenario and so it has to be easy. Just need to find the missing links.


I implemented an ActionFilterAttribute [SkipModelValidation] which avoids/eliminates unwanted model validation for search pages.

Ref: How to disable validation in a HttpPost action in ASP.NET MVC 3?

public class SkipModelValidationAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //Get ModelState
            ModelStateDictionary dict = ((Controller)filterContext.Controller).ModelState;

            if (dict != null && !dict.IsValid)
                dict.Clear();

            base.OnActionExecuting(filterContext);
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号