There were some requirements couple of weeks back which needs to return 'true' or 'false' from a MVC action based on some conditions say user has alreay registered or not. I first searched for any built-in PrimitiveResult in MVC 3 framework. But not able to find suitable one. Tried to write one myself. But before that I thought I should check for any other way to avoid reinvention of the wheel :-). That made me start digging about the ViewResult where we can return anything without type checking. Yes that worked and below are the steps to have a ViewResult which returns primitive types as string.
Action which returns the ViewResult with the primitive type as model
Just look at the code
View which renders the model without master page or any other html element
Had to struggle little bit here as I was not familiar with how to avoid the influence of master page which was mentioned in the _viewstart.cshtml .Its simple..
TestPrimitiveResult.cshtml
Now I am almost in a stage like any other MVC developer who thinks Asp.Net web application should be done through MVC only.ie ASP.Net web application => ASP.Net MVC.
Happy coding...
Action which returns the ViewResult with the primitive type as model
Just look at the code
public ActionResult TestPrimitiveResult(string id) { //This action just returns the input param as it is .You can have your own processing. double resultDouble; bool resultBool; ActionResult result; //Uses primitive type as model. if (double.TryParse(id, out resultDouble)) result = View(resultDouble); else if (Boolean.TryParse(id, out resultBool)) result = View(resultBool); else result = View(id); return result; }
View which renders the model without master page or any other html element
Had to struggle little bit here as I was not familiar with how to avoid the influence of master page which was mentioned in the _viewstart.cshtml .Its simple..
TestPrimitiveResult.cshtml
@{ //Removes the effect of master page. Layout = null; } @*Returns the primitive data value*@ @Model
Now I am almost in a stage like any other MVC developer who thinks Asp.Net web application should be done through MVC only.ie ASP.Net web application => ASP.Net MVC.
Happy coding...
No comments:
Post a Comment