Bind EditorTemplate file to grid combobox in Kendo UI MVC - telerik-grid

Imagine I have the grid on the file on /Views/Home/Index.cshtml:
#model IEnumerable<KendoMVCWrappers.Models.StockWebAndDetailsView>
#( Html.Kendo().Grid(Model)
.Name("Grid")
#* Other columns and dataSource in here *#
columns.Bound("QuantityToEnquiry").Filterable(false).Sortable(false)
.EditorTemplateName("QuantityToEnquiry");
})
And I have the file on /Views/EditorTemplateName/QuantityToEnquiry.cshtml ( I also tried on ~/Views/Shared/EditorTemplates/):
#(Html.Kendo().ComboBox()
.Name("QuantityToEnquiry")
.Value("1")
.BindTo(Enumerable.Range(1, 100).ToList())
On the model I have:
public class StockWebAndDetailsView
{
[UIHint("QuantityToEnquiry")]
public int QuantityToEnquiry { get; set; }
}
The data that is showing is null, a string with value "null", I don't know why.

Yo mate,
First I suggest you to put that editor template under the Shared/EditorTemplates folder. And then I suggest you to name the combobox with the same name as your property.
#(Html.Kendo().ComboBox()
.Name("QuantityToEnquiry")
.Value("1")
.BindTo(Enumerable.Range(1, 100).ToList())
I hope it helps.

Related

How to send Model to _Layout.cshtml (I need to send another model to Index view too)

I need to send 2 different Models, one to Index view and another one to _Layout.cshtml, how I can do it?
My HomeController:
[Route("")]
public ActionResult Index()
{
HomeViewModel model = new HomeViewModel();
model.A = _repoA.GetLatest(4);
model.B = _repoB.GetLatest(4);
model.C = _repoC.GetLatest(4);
return View(model);
}
I don't like using ViewBag, ViewData & ..., I'm looking for passing the model in same way as we passing model to Views.
You can place this in your Layout to load a partial each time... Pretty useful for loading in a piece of a dynamic menu or a widget on each page.
Along with this line in your layout you can just do your Index page as you normally would.
#{ Html.RenderAction("_widget", "Home"); }
You'll need to send it along in the ViewBag. I found the best bet was to make an abstract controller:
public abstract class ApplicationController : Controller
{
protected ApplicationController()
{
UserStateViewModel = new UserStateViewModel();
//Modify the UserStateViewModel here.
ViewBag["UserStateViewModel"] = UserStateViewModel;
}
public UserStateViewModel UserStateViewModel { get; set; }
}
Then, have all of your controllers inherit from this abstract controller.
In your _Layout.cshtml (or whatever you called it), you'll need to include the following at the top:
#{
var userState = (UserStateViewModel)ViewBag.UserStateViewModel;
}
Duplicate but refined from the 2nd answer to ASP.NET MVC Razor pass model to layout.

MVC Shared partial view with a dropdown and separate controller

What I am after is having a partial view that hosts a dropdown list of all available languages in the system. This partial view will be used in many edit templates and will be loaded from a separate controller.
Following the articles and information on the net I have the following implementation:
ViewModel
public class LanguagesViewModel
{
public int SelectedID { get; set; }
public virtual SelectList Languages { get; set; }
public LanguagesViewModel(int selectedID, IEnumerable<Language> languages)
{
SelectedID = selectedID;
Languages = new SelectList(languages, "LanguageId", "Name");
}
}
In the Shared folder I have a file: _LanguageDropDownList.cshtml with
#model XNETProductQuote.Web.Models.LanguagesViewModel
#Html.DropDownListFor(model => model.SelectedID, Model.Languages)
I have a LanguageController that has the following implementation
public ActionResult GetAllLanguages()
{
var languages = service.GetAll();
return PartialView("_LanguageDropDownList", new LanguagesViewModel(1, languages));
}
So the above is meant to load the drop down list in that partial view so I can use it in other templates.
From a template that is loaded from a different controller (ApplicationSetting) I call the partial view using:
#Html.Action("GetAllLanguages", "LanguageController")
This doesn't work. It throws an exception:
The controller for path '/ApplicationSetting/Edit/1' was not found or does not implement IController.
What is the correct implementation for such scenario?
Thanks
In Asp.Net MVC when we make a new controller then 'Controller' postfix is automatically attached to the Controller Name for ex:- in your case if you give 'Language' name to the controller then controller's complete name will be like 'LanguageController',so where ever you want to give controller name you have to use only 'Language' not 'LanguageController' and one of the overloads of #Html.Action() is ControllerName which is only 'Language' and not 'LanguageController' ,So in your problem just change #Html.Action("GetAllLanguages", "LanguageController") with #Html.Action("GetAllLanguages", "Language") and your problem will be solved.

ASP.NET MVC DropDownListFor not selecting the correct value

I'm new in the ASP.NET Framework, I've read the fundamental and have some understanding(theory) on the framework but not much in practice.
I'm struggling with the dropdownlistfor helper method, it comes down to having a weird behavior when i attempt to change the value of the selected item programatically.
In my controller i have the Index action method that receives a parameter of type Tshirt, inside this action method i set the property named Color of the Tshirt object with a value of 2.
In the view (strongly typed) i have a list of colors and pass this list as an argument for the constructor of the ColorViewModel class that will be in charge of returning SelectListItems for my list of colors.
In the view I then set the Selected property of my ColorViewModel object with the value coming from model.Color, now i have set everything so that when i call
#Html.DropDownListFor(x => x.Color, cvm.SelectItems, "Select a color")
I will have my dropdown displayed with the selected item.
When the request(GET) is performed the page is rendered by the browser and the dropdownlist appears with the correct value selected that was established with the value 2 in the Index action method, the dropdown displays "Blue" this is correct.
Then if i select a different item in the dropdownlist (RED, having an id of one) and submit the form, the Save action method is called and i know that the model is reaching the action method with a model.Color=1, which is correct.
Then i decide to redirect to the index action method passing the model object, the index action method changes the Color property back to 2, so when the page is rendered it should display again the value of Blue in the dropdown, but it doesn't, it displays Red.
if you comment out the following line in the Save action method you will get a different behavior.
//tshirt.Color = 3;
i know this logic im following doesnt make much sense from a bussines logic perspective, im just trying to understand what i am doing wrong to not get the expected result.
Given the following model
public class Color
{
public int Id { get; set; }
public string Description { get; set; }
}
I Create the following view model
public class ColorViewModel
{
public int Selected { get; set; }
private List<Color> Colors;
public IEnumerable<SelectListItem> SelectItems { get { return new SelectList(this.Colors, "Id", "Description", this.Selected); } }
private ColorViewModel() { }
public ColorViewModel(List<Color> colors)
{
this.Colors = colors;
}
}
This is my Controller
public class HomeController : Controller
{
[HttpGet()]
public ActionResult Index(Tshirt tshirt)
{
tshirt.Color = 2;
Tshirt t = new Tshirt();
t.Color = tshirt.Color;
return View(t);
}
[HttpPost()]
public ActionResult Save(Tshirt tshirt)
{
//tshirt.Color = 3;
return RedirectToAction("Index", tshirt);
//return View("Index",tshirt);
}
}
And Finally my View
#{
List<Color> colors = new List<Color>(){
new Color(){Id=1, Description="Red"},
new Color(){Id=2, Description="Blue"},
new Color(){Id=3, Description="Green"}
};
ColorViewModel cvm = new ColorViewModel(colors) { Selected = Model.Color };
}
#using(#Html.BeginForm("Save","Home")){
#Html.DropDownListFor(x => x.Color, cvm.SelectItems, "Select a color")
<input type="submit" />
}
I have uploaded the complete code: VS Solution
Because when you redirect, you are passing the updated model
return RedirectToAction("Index", tshirt);

Creating Custom Widget in Visual Studio in Orchard 1.7

I am trying to build a very simple widget with two input fields Name and Email address. Right now I am following http://www.deepcode.co.uk/search?q=how+to+build+a+widget+without+a+model+in+Orchard.
Migration.cs
public int UpdateFrom22()
{
ContentDefinitionManager.AlterTypeDefinition("QuickLinkWidget", builder => builder
.WithPart("QuickLinkWidgetPart")
.WithPart("CommonPart")
.WithPart("WidgetPart")
.WithSetting("Stereotype", "Widget"));
return 23;
}
ContentPartRecord (Models/QuickLinkWidgetRecord.cs)
public class QuickLinkWidgetRecord : ContentPartRecord
{
//We shall not define any property for this class.
//We shall not create any HANDLER for this widget because we do not need to store any data to our database.
}
ContentPart: (Models/QuickLinkWidgetPart.cs)
public class QuickLinkWidgetPart : ContentPart<QuickLinkWidgetRecord>
{
//Again we shall not definte any Record property here......
//We shall not create any HANDLER for this widget because we do not need to store any data to our database.
}
Handler
Did not define any as I don't have deal with any repository.
Driver: ((Models/QuickLinkWidgetDriver.cs))
public class QuickLinkWidgetDriver : ContentPartDriver<QuickLinkWidgetPart>
{
override DriverResult Display(QuickLinkWidgetPart part, string displayType, dynamic shapeHelper)
{
//We do not have any property defined in Model class.
//So we don't have to assign any Default values to anything!
//We shall just return a SHAPE of the widget
}
}
Placement.info:
<Place Widgets_QuickLinkWidget="AsideFirst:1" />
And finally the View: Views/Widgets/QuickLinkWidget.cshtml
#using Emfluence.Intrust.Models;
#model Contact
<h2>Our Side link widget</h2>
Nothing extra I added in my View
Please help me understanding what I am doing wrong. Why I am not able to see the widget rendered at all? I am stuck with it for the last 3 days!, just want to see it rendering first!
Thanks in advance fro your help
I had similar issue and i got it working by changing some setting on my placement.info file...in your case try this
<Place Widgets_QuickLinkWidget="Content" />

Compact Framework 3.5 Update Usercontrol With Main Form Variable Data

This seems like it should be simple, however, I cannot find an example through searching...
How do I update a label on a usercontrol using variables from its parent form?
I am building a compact framework 3.5 application.
Thanks for any and all assistance!!!
You can create a public property on the user control, and you update your label from there, for example:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public string LabelText
{
get
{
return label1.Text;
}
set
{
label1.Text = value;
}
}
}
You can also return the label itself, although some people may say that would break encapsulation on your design.

Resources