Can i create KendoUI controls in my Controller - kendo-ui

Can i create KendoUI ASP.NET MVC wrapper controls in my Controller class and pass them on to the views. And can i also display them there on the views.
for example:
public ActionResult Index()
{
GridBuilder<CustomerViewModel> grid
= new GridBuilder<CustomerViewModel>(new Grid<CustomerViewModel>(
new ViewContext(), // <-- view is empty
DI.Current.Resolve<IJavaScriptInitializer>(),
DI.Current.Resolve<IUrlGenerator>(),
DI.Current.Resolve<IGridHtmlBuilderFactory>()));
ViewBag.g = grid;
return View();
}

You could create a method that uses the kendo gridbuilder type:
public static Kendo.Mvc.UI.Fluent.GridBuilder<T> RenderGrid<T>(this HtmlHelper helper, string gridName)
where T : YourObject
{
return helper.Kendo().Grid<T>().Do stuff here;
}
And you would call it in your razor markup:
#RenderGrid("GridName")

Related

Return view into an Iframe with MVC3

I'm new with MVC and I need to put into an iframe a view which returns the content of a page( styles/css,text/html,images etc) and the view to containt the main menu of the application, from the Index view.
Can anyone help me with this?
The code line which returns the content in the view is :
#Html.Raw(System.Text.Encoding.ASCII.GetString(#Model.Content))
Just try to create controller with action that returns your view.
public class MyController
{
public ActionResult Index()
{
return View();
}
}
index view content:
#Html.Raw(System.Text.Encoding.ASCII.GetString(#Model.Content))
After that
<iframe src="localhost/mycontroller">
</iframe>

How to pass the full Model from view to controller via jquery in a MVC c# application

I have a list of checkbox's and textbox's. I want to let the user add items to the list via a partial view modal popup.
After the user adds an item to the list, if any items on the original view have values in them, I want them preserved, and the page refreshed with the added items.
I want to send the full model back to the controller from the original view, I can then just add the new items to that model and pass the model back to the original page and have all my values preserved.
I could grab all the values and pass them via loops and such in javascript (very tedious), but I think the full model would be the easiest way.
I sawe a link from Laviak on a post from here..., but I can't get it to work.
it states....
If you need to send the FULL model to the controller, you first need the model to be available to your javascript code.
In our app, we do this with an extension method:
public static class JsonExtensions
{
public static string ToJson(this Object obj)
{
return new JavaScriptSerializer().Serialize(obj);
}
}
On the view, we use it to render the model:
<script type="javascript">
var model = <%= Model.ToJson() %>
</script>
You can then pass the model variable into your $.ajax call.
Has anyone got this to work???
Thanks
Bill
you can do something like this:
<script type="text/javascript">
var dataViewModel = #Html.Raw(Json.Encode(Model)); //Make sure you send the proper model to your view
function MethodPost(param1, param2, etc...) {
dataviewModel.Param1 = param1; //Or load a value from jQuery
dataviewModel.Param2 = $("#param2").val();
}
//Pass it to a controller method
$.post("#Url.Action(MVC.Home.PostMethodInController())", { viewModel: JSON.stringify(dataViewModel)} , function(data) {
//Do something with the data returned.
}
</script>
In the controller you get your class/model using Json.Net which is available on nuget.
public virtual ActionResult Index()
{
return View(new MyModelToView());//Send at least an empty model
}
[HttpPost]
public virtual JsonResult PostMethodInController(string viewModel)
{
var entity = JsonConvert.DeserializeObject<MyObject>(viewModel);
//Do Something with your entity/class
return Json(entity, JsonRequestBehavior.AllowGet);
}
Hope this helps.

How to return a partial view in a controller in ASP.NET MVC3?

I have a controller and one of its methods (actions) access my database of items. That method checks the item type. How do I show my partial view only if the item retrieved from my database is of a specific type?
Controller Action example:
public ActionResult CheckItem(Koko model)
{
var item = db.Items.Where(item => item.Number == model.Number).First();
if(item.Type=="EXPENSIVE")
{
//show partial view (enable my partial view in one of my Views)
}
}
You could return a PartialView action result:
public ActionResult CheckItem(Koko model)
{
var item = db.Items.Where(item => item.Number == model.Number).First();
if (item.Type=="EXPENSIVE")
{
return PartialView("name of the partial", someViewModel);
}
...
}
Now the controller action will return partial HTML. This obviously means that you might need to use AJAX in order to invoke this controller action otherwise you will get the partial view replace the current browser window. In the AJAX success callback you could reinject the partial HTML in the DOM to see the update.

How to pass variables across views and partial views in mvc 3?

I am creating a breadcrumb partial view which takes in a collection of title/URL. The collection will be generated in action methods and would have to be available on the breadcrumb partial view.
I tried couple of ways to get it done and this is one among such: http://goo.gl/rMFlp
But some how i could not get it working. All i get is an "Object reference not set to an instance of an object." Can you guys help me out?
{Updare}
Here is the code:
I created a Model class as follows
public class ShopModel
{
public Dictionary<string,string> Breadcrumb { get; set; }
}
Action Method
public ActionResult Index()
{
var breadcrumbCollection = new Dictionary<string,string>();
breadcrumbCollection.Add("/home","Home");
breadcrumbCollection.Add("/shop","Shop");
var model = new ShopModel() { Breadcrumb = breadcrumbCollection};
return View(model);
}
Model binding the view - Index
#Model NexCart.Model.Model.Custom.ShopModel
Finally here is the code on partial view:
<div>
#{
foreach (var item in #Model.Breadcrumb)
{
#item.Key
}
}
You haven't shown any code, so your question is impossible to answer. This being said here's how you could proceed. As always in an ASP.NET MVC application you start by defining a view model:
public class Breadcrumb
{
public string Title { get; set; }
public string Url { get; set; }
}
then you could write a controller action which will populate a collection of breadcrumbs and pass them to a partial view:
public class BreadcrumbController: Controller
{
public ActionResult Index()
{
// TODO: pull the breadcrumbs from somewhere instead of hardcoding them
var model = new[]
{
new Breadcrumb { Title = "Google", Url = "http://www.google.com/" },
new Breadcrumb { Title = "Yahoo", Url = "http://www.yahoo.com/" },
new Breadcrumb { Title = "Bing", Url = "http://www.bing.com/" },
};
return PartialView(model);
}
}
then you could have a corresponding partial view which will render this model (~/Views/Breadcrumb/Index.cshtml):
#model IEnumerable<Breadcrumb>
<ul>
#Html.DisplayForModel()
</ul>
and the corresponding display template (~/Views/Breadcrumb/DisplayTemplates/Breadcrumb.cshtml):
#model Breadcrumb
<li>
#Model.Title
</li>
Now all that's left is to include this child action somewhere using the Html.Action helper. For example you could do this in the _Layout if this breadcrumb is repeated on each page:
#Html.Action("Index", "Breadcrumb")
But obviously it could also be done in any view.

Dynamic Layout for Error View in ASP.Net MVC

I have two action called "a" and "b". also I have two view for them. the layout of these views is difference.
for a:
#{
Layout = "~/Views/Shared/_X.cshtml";
}
for b:
#{
Layout = "~/Views/Shared/_Y.cshtml";
}
also the Error view is shared.
How can I use a dynamic layout for Error view. for example when an error occurred while processing action "a" the error show in layout of action "a" and if an error occurred while processing action "b" the error show in layout of action "b"?
You could write a helper method:
public static string GetLayout(this HtmlHelper htmlHelper)
{
var action = htmlHelper.ViewContext.RouteData.GetRequiredString("action");
if (string.Equals("a", action, StringComparison.OrdinalIgnoreCase))
{
return "~/Views/Shared/_X.cshtml";
}
else if (string.Equals("b", action, StringComparison.OrdinalIgnoreCase))
{
return "~/Views/Shared/_Y.cshtml";
}
return "~/Views/Shared/_Layout.cshtml";
}
and then:
#{
Layout = Html.GetLayout();
}
How about this overload?
Controller.View Method (String, String) (System.Web.Mvc)
in a action
return View(viewName,"_X");
in b action
return View(viewName,"_Y";
hope this help u....various way of rendering layouts in Asp.Net MVC.
Method 1 : Control Layouts rendering by using _ViewStart file in the root directory of the Views folder
We can change the default rendering of layouts with in _ViewStart file by using the below code:
#{
var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"].ToString();
string layout = "";
if (controller == "Admin")
{
layout = "~/Views/Shared/_AdminLayout.cshtml";
}
else
{
layout = "~/Views/Shared/_Layout.cshtml";
}
Layout = layout;
}
Method 2 : Return Layout from ActionResult
We can also override the default layout rendering by returning the layout from the ActionResult by using the below code:
public ActionResult Index()
{
RegisterModel model = new RegisterModel();
//TO DO:
return View("Index", "_AdminLayout", model);
}
Method 3 : Define Layout with in each view on the top
We can also override the default layout rendering by defining the layout on the view by using the below code:
#{
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
thanks
You could try and pass the layout from the controller action: set the current layout in the session and then retrieve it in your error controller and pass it to the view through the ViewBag property:
public ActionResult A()
{
// Do things
// Set the layout
Session["currentLayout"] = "~/Views/Shared/_X.cshtml";
return View();
}
ErrorController:
public ActionResult NotFound() // 404
{
// Set the layout
ViewBag.ErrorLayout = Session["currentLayout"];
return View();
}
then in your error view:
#{
Layout = ViewBag.ErrorLayout;
}
I'll grant you this will not win a beauty prize; there might be other ways.
For example, take a look at this answer for how to set the layout in an ActionFilter: How to set a Razor layout in MVC via an attribute filter?
You could write your own Error Filter inheriting from HandleError and set the layout in there.

Resources