views:

39

answers:

1

Hi,

I am trying to get whether it is a post or get in the htmlhelper as i am building an validationsummary extension and I only want it to run when the form is posted?

Any ideas?

currently what I've got is this and it works well but it runs every time the page is loaded

StringBuilder sb = new StringBuilder();

        sb.Append("<script type='text/javascript'>");
        sb.Append("$(function() {");


        if (htmlHelper.ViewData.ModelState.IsValid) {
            sb.Append("LLNP4.addSuccess()");
            sb.Append("});");
            sb.Append("</script>");
            return sb.ToString();
        }



        foreach (ModelState modelState in htmlHelper.ViewData.ModelState.Values){
            foreach (ModelError modelError in modelState.Errors){
                sb.Append("LLNP4.addError('" + modelError.ErrorMessage + "' );");

            }
        }

        sb.Append("});");
        sb.Append("</script>");

        return sb.ToString();
A: 

You can use HttpContext.Current.Request.HttpMethod, but I feel that your idea doesn't seem right.

EDIT

Make sure that your error messages don't contain html tags or encode error messages first.

LukLed
Yeah I get an itching/burning sensation when I llok at the idea also. Think this would be better handled elsewhere.
griegs
'handled elsewhere' - where?I am building a htmlhelper extension for the validation summary. I'd be more than happy to hear any other approaches :)
kurasa
@kjm: Validation summary as html helper is ok, but checking if it is POST doesn't feel right. Specially when you consider using POST-REDIRECT-GET pattern.
LukLed
thanks for both comments. I'll consider them and keep an eye out for any problems.
kurasa