I am using the Sort method of the MvcContrib Grid to generate sorting links, e.g.
<%= Html.Grid(Model).AutoGenerateColumns().Sort((GridSortOptions)ViewData["sort"]) %>
I have a need to change the default controller/action that’s generated by the sort method. For example,
defaultControllerName/defaultActionName/?Column=ProductId&Direction=Ascending
would change to
customControllerName/customActionName/?Column=ProductId&Direction=Ascending
I haven't been able to find any existing methods in the MVCcontribution classes that would allow me to customise the links. I’d appreciate any pointers on how to go about altering the default links as I’m still very much a MVC/C# newbie.
That's not an easy task. You will need a custom grid renderer to achieve this and override the RenderHeaderText method:
public class MyHtmlTableGridRenderer<T> : HtmlTableGridRenderer<T> where TViewModel : class
{
protected override void RenderHeaderText(GridColumn<TViewModel> column)
{
if (IsSortingEnabled && column.Sortable)
{
// TODO: generate a custom link here based on the sorting options
string text = ...
base.RenderText(text);
}
else
{
RenderText(column.DisplayName);
}
}
}
And then specify that the grid should use this renderer:
.RenderUsing(new MyHtmlTableGridRenderer<Employee>())
I wanted to provide a complete working example:
public class SortableHtmlTableGridRenderer<T> : HtmlTableGridRenderer<T> where T : class
{
readonly string _action;
readonly string _controllerName;
public SortableHtmlTableGridRenderer(string action, string controllerName)
{
_action = action;
_controllerName = controllerName;
}
protected override void RenderHeaderText(GridColumn<T> column)
{
if (IsSortingEnabled && column.Sortable)
{
string sortColumnName = GenerateSortColumnName(column);
bool isSortedByThisColumn = GridModel.SortOptions.Column == sortColumnName;
var sortOptions = new GridSortOptions
{
Column = sortColumnName
};
if (isSortedByThisColumn)
{
sortOptions.Direction = (GridModel.SortOptions.Direction == SortDirection.Ascending)
? SortDirection.Descending
: SortDirection.Ascending;
}
else //default sort order
{
sortOptions.Direction = column.InitialDirection ?? GridModel.SortOptions.Direction;
}
var routeValues = HtmlHelper.AnonymousObjectToHtmlAttributes(new {sortOptions.Column, sortOptions.Direction });
var text = HtmlHelper.GenerateLink(Context.RequestContext, RouteTable.Routes, column.DisplayName, null, _action, _controllerName, routeValues, null);
RenderText(text);
}
else
{
RenderText(column.DisplayName);
}
}
}
Usage:
.RenderUsing(new SortableHtmlTableGridRenderer<YourModelType>("Search", "Search"))
Related
I'm using knockout mapping to help map a serverside object into JSON. I have an object with numerous collections in it so I don't want to have to recreate and map each piece manually in javascript. So, I'm using knockout-mapping to do this for me.
I was having issues, so I decided to try it with a simple example, so here is what I have for an ASP.NET MVC application:
C# Model:
public class Vaccinations
{
public string Vaccination { get; set; }
public System.DateTime VaccinationDate { get; set; }
}
public class Dog
{
public string Name { get; set; }
public int Age { get; set; }
public Dog()
{
this.Vaccinations = new System.Collections.Generic.List<Vaccinations>();
}
public System.Collections.Generic.List<Vacinations> Vacinations { get; set; }
}
As you can see, each Dog has a list of vaccinations they may or may not have.
In my controller, I create and return a pre-populated Dog object:
public ActionResult Load()
{
Dog rambo = new Dog
{
Name = "Rambo",
Age = 5
};
rambo.Vacinations = new System.Collections.Generic.List<Vacinations> {
new Vacinations { Vacination = "Rabies", VacinationDate = DateTime.Now.AddYears(-1) },
new Vacinations { Vacination = "Mumps", VacinationDate = DateTime.Now.AddYears(-2) }
};
return Json(rambo, JsonRequestBehavior.AllowGet);
}
In my view (Index.cshtml), I have it set up to show the dog and a list of it's vaccinations. I want to allow the user to click on an Add Vaccination button to add a new line to the collection and allow them to enter the data.
Again, I'm using knockout.mapping to do the mapping for me. In the javascript section, this is what I have:
var ViewModel = function (data) {
var self = this;
ko.mapping.fromJS(data, {}, self);
self.isValid = ko.computed(function () {
return self.Name().length > 3;
});
// Operations
self.save = function () {
$.ajax({
url: "Dog/Save",
type: "post",
contentType: "application/json",
data: ko.mapping.toJSON(self),
success: function (response) {
alert(response.Status);
}
});
};
self.addVaccination = function () {
self.Vaccinations.push(new self.Vaccination()); // <--- This doesn't work and I know why, so how do I do this?
}
};
$(function () {
$.getJSON("Dog/Load", null, function (data) {
ko.applyBindings(new ViewModel(data));
});
});
My question revolves around the "addVaccination" function that I've added to the ViewModel object. How do I specify a new "Vaccination" object without having to "code" one in Javascript? That was the entire reason for me using knockout mapping so I don't have to do that. But, I don't see any other way around it.
Is there a way to access the base Vaccination object from the Vaccinations observable array so I can create a new one?
And then the final question is, how to I pass this back to my controller? I'm not sure if this will work or not.
You can't directly. But what you can do is define a Vaccination instance at the server side and return it as a the default instance.
So, you need to return the old data and the default instance.
public ActionResult Load()
{
...
var data = new {
defaultVacination = new Vacination(),
rambo = rambo,
};
return Json(data , JsonRequestBehavior.AllowGet);
}
And on the client side you receive the same data and the default instance.
var ViewModel = function (data) {
var self = this;
ko.mapping.fromJS(data.rambo, {}, self);
var defaultInstance = data.defaultVacination;
...
self.addVaccination = function () {
// clone the default instance.
self.Vaccinations.push(ko.utils.extend({}, defaultInstance));
}
I hope it helps.
I am currently trying to write a custom authentication filter and I need to access the dto that is being passed as a parameter to the action in my filter. Lets say I have an action like this
[AuthenticateProfile]
public ActionResult EditProfile(ProfileDTO profileDto)
{
if (ModelState.IsValid)
{
// Do crazy stuff
}
return something....
}
I need to do my authentication based on some of the properties that are inside profiledto object.
I want to know how I can get this object inside my filter from AuthorizationContext.
Here's how I did it:
var parameters = filterContext.ActionDescriptor.GetParameters();
var values = parameters.Select(s => new
{
Name = s.ParameterName,
Value = filterContext.HttpContext.Request[s.ParameterName]
});
Assuming that your logic is happening in OnActionExecuting (meaning before the actual controller action is run), then one way of doing this (as outlined here) would be:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!filterContext.Controller.ViewData.ModelState.IsValid)
return;
var profileDto = filterContext.ActionParameters.SingleOrDefault(ap => ap.Key == "profileDto").Value;
if (profileDto != null)
{
// do something with profileDto
}
}
ASP.NET Core
var parameters =
filterContext
.ActionDescriptor
.Parameters
.Select(s => new
{
Name = s.Name,
Value = context.HttpContext.Request.Query[s.Name]
});
extension
public static StringValues? Parameter(this AuthorizationFilterContext context, string name)
{
var parameter = context.ActionDescriptor.Parameters.FirstOrDefault(it => it.Name == name);
if (parameter == null) return null;
return context.HttpContext.Request.Query[parameter.Name];
}
use
var parameter = context.Parameter("id");
Here are some examples for you to try out:
public class AuthenticateProfileAttribute : AuthorizeAttribute {
public override void OnAuthorization(AuthorizationContext filterContext) {
// Looping through named parameters
foreach (string name in filterContext.HttpContext.Request.QueryString.AllKeys) {
var value = filterContext.HttpContext.Request.QueryString[name];
// do something with the iteration
Console.WriteLine(name + ": " + value);
}
// Looping through un-named parameters a.k.a route parameters
foreach (KeyValuePair<string, object> parameter in filterContext.RouteData.Values) {
var name = parameter.Key;
var value = parameter.Value;
// do something with the iteration
Console.WriteLine(name + ": " + value);
}
// Get single named parameter
string parameter = filterContext.HttpContext.Request.QueryString["parameter"];
// Get single route parameter
parameter = filterContext.RouteData.Values["parameter"].ToString();
}
}
Using MVC 5.
Good day!
I am working on a project right now and I am using JQGrid to show my data. As part of its functions, users can choose columns and make these columns as their default columns.
I used 'columnChooser' to let my user select for their default columns.
My problem now, how to retrieve those columns the user's selected?
And how to set those columns as their default columns?
Can someone help me with this problem.
Thanks
Jason
After the user changes the column layout, you can get the colModel from the grid, iterate through it and push the configuration in a array of json objects which will then be sent to the server. The following code does this:
function saveColumnConfiguration(grid, url) {
if (url.length > 0) {
var colArray = new Array();
var colModel = grid[0].p.colModel;
for (var i = 0; i < colModel.length; i++) {
if (colModel[i].name != "rn" && colModel[i].name != "cb") {
colArray.push({
Name: colModel[i].name,
Width: colModel[i].width,
Visible: !colModel[i].hidden
});
}
}
$.ajax({
url: url,
type: 'POST',
data: 'columnConfiguration=' + JSON.stringify(colArray)
});
}
}
The check for "rn" and "cb" means don't take the rownumber and checkbox columns.
UPDATE
You will need a class to represent the columns:
[Serializable]
public class JqGridColumn
{
public string Name { get; set; }
public int Width { get; set; }
public bool Visible { get; set; }
}
You also need custom model binder to deserialize the incoming list:
public class JqGridConfigurationModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var conf = bindingContext.ValueProvider.GetValue("columnConfiguration").AttemptedValue;
JavaScriptSerializer serializer = new JavaScriptSerializer();
var configuration = serializer.Deserialize<IEnumerable<JqGridColumn>>(conf);
return configuration;
}
}
Register the model binder in application start:
ModelBinders.Binders.Add(typeof(IEnumerable<JqGridColumn>), new JqGridConfigurationModelBinder());
The action in the controller that handles the list will be something like this:
public void SaveColumnConfiguration(IEnumerable<JqGridColumn> columnConfiguration)
{
// Save the list accordingly...
}
Note that the order of the columns is represented by their position in the list.
You can then easily read this configuration and render the grid.
UPDATE 2
The function in your case should be called like this
saveColumnConfiguration($("#freight_bill"), "/Controller/Action");
But not after the call for columnChooser. You can either make another button to save changes when the user chooses to do so or handle the done event from the column chooser like this:
$("#freight_bill").jqGrid('columnChooser', {
done: function (perm) {
if (perm) {
$("#freight_bill").jqGrid("remapColumns", perm, true, false);
}
saveColumnConfiguration($("#freight_bill"), "/Controller/Action");
}
});
I am using ASP.NET MVC 2 Beta. I can create a wizard like workflow using Steven Sanderson's technique (in his book Pro ASP.NET MVC Framework) except using Session instead of hidden form fields to preserve the data across requests. I can go back and forth between pages and maintain the values in a TextBox without any issue when my model is not a collection. An example would be a simple Person model:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
}
But I am unable to get this to work when I pass around an IEnumerable. In my view I am trying to run through the Model and generate a TextBox for Name and Email for each Person in the list. I can generate the form fine and I can submit the form with my values and go to Step2. But when I click the Back button in Step2 it takes me back to Step1 with an empty form. None of the fields that I previously populated are there. There must be something I am missing. Can somebody help me out?
Here is my View:
<% using (Html.BeginForm()) { %>
<% int index = 0;
foreach (var person in Model) { %>
<fieldset>
<%= Html.Hidden("persons.index", index.ToString())%>
<div>Name: <%= Html.TextBox("persons[" + index.ToString() + "].Name")%></div>
<div>Email: <%= Html.TextBox("persons[" + index.ToString() + "].Email")%></div>
</fieldset>
<% index++;
} %>
<p><input type="submit" name="btnNext" value="Next >>" /></p>
<% } %>
And here is my controller:
public class PersonListController : Controller
{
public IEnumerable<Person> persons;
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
persons = (Session["persons"]
?? TempData["persons"]
?? new List<Person>()) as List<Person>;
// I've tried this with and without the prefix.
TryUpdateModel(persons, "persons");
}
protected override void OnResultExecuted(ResultExecutedContext filterContext)
{
Session["persons"] = persons;
if (filterContext.Result is RedirectToRouteResult)
TempData["persons"] = persons;
}
public ActionResult Step1(string btnBack, string btnNext)
{
if (btnNext != null)
return RedirectToAction("Step2");
// Setup some fake data
var personsList = new List<Person>
{
new Person { Name = "Jared", Email = "test#email.com", },
new Person { Name = "John", Email = "test2#email.com" }
};
// Populate the model with fake data the first time
// the action method is called only. This is to simulate
// pulling some data in from a DB.
if (persons == null || persons.Count() == 0)
persons = personsList;
return View(persons);
}
// Step2 is just a page that provides a back button to Step1
public ActionResult Step2(string btnBack, string btnNext)
{
if (btnBack != null)
return RedirectToAction("Step1");
return View(persons);
}
}
As far as I can tell, this is not supported in ASP.NET MVC 2 Beta, nor is it supported in ASP.NET MVC 2 RC. I dug through the MVC source code and it looks like Dictionaries are supported but not Models that are IEnumerable<> (or that contain nested IEnumerable objects) and it's inheritors like IList<>.
The issue is in the ViewDataDictionary class. Particularly, the GetPropertyValue method only provides a way to retrieve property values from dictionary properties (by calling GetIndexedPropertyValue) or simple properties by using the PropertyDescriptor.GetValue method to pull out the value.
To fix this, I created a GetCollectionPropertyValue method that handles Models that are collections (and even Models that contain nested collections). I am pasting the code here for reference. Note: I don't make any claims about elegance - in fact all the string parsing is pretty ugly, but it seems to be working. Here is the method:
// Can be used to pull out values from Models with collections and nested collections.
// E.g. Persons[0].Phones[3].AreaCode
private static ViewDataInfo GetCollectionPropertyValue(object indexableObject, string key)
{
Type enumerableType = TypeHelpers.ExtractGenericInterface(indexableObject.GetType(), typeof(IEnumerable<>));
if (enumerableType != null)
{
IList listOfModelElements = (IList)indexableObject;
int firstOpenBracketPosition = key.IndexOf('[');
int firstCloseBracketPosition = key.IndexOf(']');
string firstIndexString = key.Substring(firstOpenBracketPosition + 1, firstCloseBracketPosition - firstOpenBracketPosition - 1);
int firstIndex = 0;
bool canParse = int.TryParse(firstIndexString, out firstIndex);
object element = null;
// if the index was numeric we should be able to grab the element from the list
if (canParse)
element = listOfModelElements[firstIndex];
if (element != null)
{
int firstDotPosition = key.IndexOf('.');
int nextOpenBracketPosition = key.IndexOf('[', firstCloseBracketPosition);
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(element).Find(key.Substring(firstDotPosition + 1), true);
// If the Model has nested collections, we need to keep digging recursively
if (nextOpenBracketPosition >= 0)
{
string nextObjectName = key.Substring(firstDotPosition+1, nextOpenBracketPosition-firstDotPosition-1);
string nextKey = key.Substring(firstDotPosition + 1);
PropertyInfo property = element.GetType().GetProperty(nextObjectName);
object nestedCollection = property.GetValue(element,null);
// Recursively pull out the nested value
return GetCollectionPropertyValue(nestedCollection, nextKey);
}
else
{
return new ViewDataInfo(() => descriptor.GetValue(element))
{
Container = indexableObject,
PropertyDescriptor = descriptor
};
}
}
}
return null;
}
And here is the modified GetPropertyValue method which calls the new method:
private static ViewDataInfo GetPropertyValue(object container, string propertyName) {
// This method handles one "segment" of a complex property expression
// First, we try to evaluate the property based on its indexer
ViewDataInfo value = GetIndexedPropertyValue(container, propertyName);
if (value != null) {
return value;
}
// If the indexer didn't return anything useful, continue...
// If the container is a ViewDataDictionary then treat its Model property
// as the container instead of the ViewDataDictionary itself.
ViewDataDictionary vdd = container as ViewDataDictionary;
if (vdd != null) {
container = vdd.Model;
}
// Second, we try to evaluate the property based on the assumption
// that it is a collection of some sort (e.g. IList<>, IEnumerable<>)
value = GetCollectionPropertyValue(container, propertyName);
if (value != null)
{
return value;
}
// If the container is null, we're out of options
if (container == null) {
return null;
}
// Third, we try to use PropertyDescriptors and treat the expression as a property name
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(container).Find(propertyName, true);
if (descriptor == null) {
return null;
}
return new ViewDataInfo(() => descriptor.GetValue(container)) {
Container = container,
PropertyDescriptor = descriptor
};
}
Again, this is in the ViewDataDictionary.cs file in ASP.NET MVC 2 RC. Should I create a new issue to track this on the MVC codeplex site?
I have a controller action which retuns a ImageResult (extending ActionResult). When this action method is called the first time with parameter value N, other consequent calls will ALL have the same parameter value N, while their parameters differ. I guess this is somehow related to parameter caching in ASP.NET MVC.
As result, return image by calling the action is always the same, regardless of the parameter value. Is there a way around this?
Maybe its something related to directly writing to Response? Here's my ImageResult:
public class ImageResult : ActionResult
{
public Image Image
{
get; set;
}
public ImageFormat ImageFormat
{
get; set;
}
private static Dictionary FormatMap
{
get; set;
}
static ImageResult()
{
CreateContentTypeMap();
}
public override void ExecuteResult(ControllerContext context)
{
if (Image == null) throw new ArgumentNullException("Image");
if (ImageFormat == null) throw new ArgumentNullException("ImageFormat");
context.HttpContext.Response.Clear();
context.HttpContext.Response.ContentType = FormatMap[ImageFormat];
Image.Save(context.HttpContext.Response.OutputStream, ImageFormat);
}
private static void CreateContentTypeMap()
{
FormatMap = new Dictionary
{
{ ImageFormat.Bmp, "image/bmp" },
{ ImageFormat.Gif, "image/gif" },
{ ImageFormat.Icon, "image/vnd.microsoft.icon" },
{ ImageFormat.Jpeg, "image/Jpeg" },
{ ImageFormat.Png, "image/png" },
{ ImageFormat.Tiff, "image/tiff" },
{ ImageFormat.Wmf, "image/wmf" }
};
}
}
and the controller action:
public ActionResult GetCalendarBadge(DateTime displayDate)
{
var bmp = SomeBitmap();
var g = Graphics.FromImage(bmp);
//GDI+ to draw the image.
return new ImageResult { Image = bmp, ImageFormat = ImageFormat.Png };
}
and the view code:
<% foreach(var item in this.Model.News) { %>
<%= Html.Image<NewsController>(o => o.GetCalendarBadge(item.DisplayDate), 75, 75)%>
<% } %>
Also tried adding these two avoid caching but nothing happened:
context.HttpContext.Response.Cache.SetNoStore();
context.HttpContext.Response.Expires = 0;
context.HttpContext.Response.AppendHeader("Pragma", "no-cache");
I thought that behaviour was 'opt-in' via the caching attributes on the action.
Are you absolutely sure your data access layer is working as it should?
Found the problem! It is due to using Windsor IoC and registering / resolving the controllers as Singleton (which is the default behavior). Changing the lifestyle to Transient fixed the issue (along with other issues).
See here for more info.