Thursday, March 28, 2013

Use of NonActionAttribute


Use of NonActionAttribute in MVC


Public non-action methods in ASP.NET MVC controllers are source of problems because they can be called by user when not handled carefully. Same time you may need public methods on controllers for some other reasons (some UI framework, testability problems, things you cannot change etc). In this posting I will show you how to handle controller methods properly.

for example, in a controller you will have a ActionResult Index

 public ActionResult Index()
  {     
    ViewBag.Message = "Welcome to ASP.NET MVC!";        
    return View();
  }

and a public method for doing some action internally 

public void Internalmethod()  {    
   Response.Write("doing some internal process");      
   Response.End();  
}

But in normal case from front-end User can access both method because in MVC every method inside Controller treated as ActionResult.

eg: you can call the Internalmethod() like http://localhost:2234/Home/Internalmethod it will render that page 
in order to avoid this issue we can use NonActionAttribute to notify MVC framework that given controller method is not action


[NonAction]
public void Internalmethod()  {   

    Response.Write("doing some internal process");    
    Response.End(); 


 }

Now when we try to run Internalmethod() over URL we get the error 404 as response to request.




JSON In IE7


Hi all I had a issue of using Json object in the ie7 I spend some time in Google and found solution for it, so thought of sharing to you guys,

If you found the issue just use the Json Libreray after your main jquery call

   <!--[if IE 7]>
          <script src="@Url.Content("~/Scripts/json2.js")" type="text/javascript"></script>
    <![endif]-->


you can find the source from here

https://github.com/douglascrockford/JSON-js/blob/master/json2.js

Costume Attribute WIth paramerters

Hi All ,Here i try to explain how to create Costume attribute and pass parameter to the costume attribute in mvc


Create a class which inherited from ActionFilterAttribute (or any filter classes)


public class CheckNames : ActionFilterAttribute
    {
        public string name { get; set; }
        public int id { get; set; }

        public override void OnActionExecuting(ActionExecutingContext actionContext)

        {
            var actn = actionContext.ActionDescriptor.ActionName;
            var cntrl = actionContext.ActionDescriptor.ControllerDescriptor.ControllerName;

        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
        }
    }


And in controller you can directly call


        [CheckNames(name = "asd", id = 1)]
        public ActionResult Index()
        {
            return View();
        }




Tuesday, December 4, 2012

Passing Multidimensional Array through Ajax to Controller


In view pge Create a Ajax call  and set the property Tradition:true 


1) the call will be like

    $.ajax({
                url: "controller/Action",
                type: "POST",
                traditional: true,
                contentType: 'application/json',
                data: JSON.stringify({outerLayer :[["a","b","c"],["d","r","g"]]}),
                dataType: 'json'
            }).done(function () {
                alert("sucess");
            });

2) In the controller the action method should like

    [httpPost]
    public ActionResult SaveFlow(string[][] outerLayer )

 now automatically the value will be there in outerlayer we can iterate through the string and get the needed data


For passing normal array though the ajax and binding to the model in mvc you can visit this link.
http://www.ienablemuch.com/2011/05/pass-array-from-jquery-to-aspnet-mvc.html?showComment=1354648913792#c691247510822739097