I have model class as below:
public class HomeFilterModel {
public string CompanyName { get; set; }
public string Country { get; set; }
public DateRange Period { get; set; }
}
public class DateRange {
public DateTime? From { get; set; }
public DateTime? To { get; set; }
}
On client side, the data collected as below object:
{
"CompanyName":"",
"Country":"Canada",
"Period":{
"from":"2018-08-05T04:00:00.000Z",
"to":"2018-08-09T04:00:00.000Z"
}
}
It's converted to query string by $.param function and added to URL.
?CompanyName=&Country=Canada&Period%5Bfrom%5D=2018-08-05T04%3A00%3A00.000Z&Period%5Bto%5D=2018-08-10T04%3A00%3A00.000Z
The controller action is as below:
[HttpGet]
public ActionResult Index(HomeFilterModel filter) {
return View(filter ?? new HomeFilterModel());
}
The model data filter can get CompanyName and Country values correctly, but Period.From and Period.To are always NULL. I wonder if I have to make customized ModelBinder for this model class specifically? Or I should use different function other than $.param to construct query string in URL?
UPDATED:
If query string is changed to
?CompanyName=&Country=Canada&Period.from=2018-08-05T04%3A00%3A00.000Z&Period.to=2018-08-10T04%3A00%3A00.000Z
controller action can get all data correctly. So Period[from] is not acceptable by default model binder, while Period.from is.
Anyone has the simplest and best solution for this?
Related
I have a webapi controller with the following method signature:
public IHttpActionResult GetAttractions([FromUri] SearchAttractionRequest request)
The class SearchAttractionRequest looks like:
public class SearchAttractionRequest
{
public string Region { get; set; }
public string Category { get; set; }
public string Genre { get; set; }
}
This all works fine.
I am looking to create a new endpoint with same but this time the parameter will have fields that are guids so it will look like:
public class SearchAttractionGuidRequest
{
public Guid? Region { get; set; }
public Guid? Category { get; set; }
public Guid? Genre { get; set; }
}
And the new endpoint will be :
public IHttpActionResult GetAttractions([FromUri] SearchAttractionGuidRequest request)
Given that the parameters are coming from query string would the binding be able to bind? At the querystring level it's all strings. Ideally, I want to avoid creating a method with a new name.
Currently when i try this i get:
Multiple actions were found that match the request: GetAttractions
You can avoid creating a new method name by specifying different [Route] attributes for those methods.
I'm trying to implement server-side paging and sorting for jquery-datatable.
but the issue is I'm not able to bind data posted by datatable to my action model
to do sort and filter
Here is the data posted by jquery-datatable ajax request
draw:5
columns[0][data]:FirstName
columns[0][name]:FirstName
columns[0][searchable]:true
columns[0][orderable]:true
columns[0][search][value]:
columns[0][search][regex]:false
columns[1][data]:LastName
columns[1][name]:LastName
columns[1][searchable]:true
columns[1][orderable]:true
columns[1][search][value]:
columns[1][search][regex]:false
......
columns[n][data]:Position
columns[n][name]:Position
columns[n][searchable]:true
columns[n][orderable]:true
columns[n][search][value]:
columns[n][search][regex]:false
order[0][column]:1
order[0][dir]:desc
start:0
length:10
search[value]:
search[regex]:false
and my action method is:
public JsonResult GetGridData(GridFilter filter)
{ ....}
and my model classes are
public class GridFilter
{
public int draw { get; set; }
public List<ColModel> columns { get; set; }
public List<Order> order { get; set; }
public int start {get;set;}
public int length {get;set;}
public search search { get; set; }
}
public class ColModel
{
public string data { get; set; }
public string name { get; set; }
public string searchable { get; set; }
public string orderable { get; set; }
}
public class Order
{
public string dir { get; set; }
public string column { get; set; }
}
public class search
{
public string value {get;set;}
public string regex {get;set;}
}
How can I make data bind properly using default mvc model binders are a custom one.
Thanks
Make sure your model properties have the same data types as defined here.
Also you have gone one level too far with your models.They are sent as individual parameters so you don't need the GridFilter model, they should be received like so:
[HttpPost]
public JsonResult GetGridData(List<ColModel> columns, List<Order> order, Search search, int? start, int? length, int? draw)
{
}
Hope someone can help - this has been bugging me for around 2 hours - its probably something simple :)
Kendo UI Grid sends a request to my controller
http://localhost:1418/user/update?UserID=1&UserName=Admin&RoleName=Admin&Email=c.j.hannon%40gmail.com&Active=true&Company%5BCompanyID%5D=1&Company%5BCompanyName%5D=asd
However, the controller class 'Company' isnt bound by the binder? Can any one help my view model and controller action signature are below:
[HttpGet]
public JsonResult Update(UserViewModel model)
{
svcUser.UpdateUser(new UpdateUserRequest() {
UserID=model.UserID,
RoleID = model.RoleName,
Email = model.Email,
Active = model.Active.GetValueOrDefault(false),
UserName = model.UserName
});
return Json("", JsonRequestBehavior.AllowGet);
}
public class UserViewModel
{
public int UserID { get; set; }
public string UserName { get; set; }
public string RoleName { get; set; }
public string Email { get; set; }
public bool? Active { get; set; }
public CompanyViewModel Company { get; set; }
}
Cheers
Craig
A few things. Your immediate problem is that Company is mapped to a complex object not a primitive type. Kendo Grid just does not do this (as of this writing). Just guessing, but you probably want to setup a foreign key binding on the Grid and just pass back the Id of the company from a listbox. This is not as bad as you think and it will immediatly fix your problem and look nice too.
Maybe personal taste but seems to be a convention. Use the suffix ViewModel for the model that is bound to your View and just the suffix Model for your business objects. So a Kendo Grid is always populated with a Model.
Ex.:
public class UserModel
{
public int UserID { get; set; }
public string UserName { get; set; }
public string RoleName { get; set; }
public string Email { get; set; }
public bool? Active { get; set; }
public int CompanyID { get; set; }
}
public class CompanyModel
{
public int ID { get; set; }
public string Name { get; set; }
}
public class UserViewModel
{
public UserModel UserModel { get; set; }
public IList<CompanyModel> Companies { get; set; }
}
public ActionResult UserEdit(string id)
{
var model = new UserViewModel();
model.UserModel = load...
model.Companies = load list...
return View(model);
}
#model UserViewModel
...
column.ForeignKey(fk => fk.CompanyId, Model.Companies, "ID", "Name")
(Razor Notation)
BUT! This is just an example, you are better off Ajax loading the Grid with the IList becuase I assume you have many Users in the Grid at once, though you could server bind off the ViewModel with a List too. But the list of Companies is probably the same every time, so map it to the View just liek this rather than Ajax load it every time you do a row edit. (not always true)
I have a class, which has 8 properties / 8 columns in DB. In the Edit page, I want to exclude the AddedDate and UserID fields. When a user edits a voucher, he can't overwrite the AddedDate or UserID values in the DB.
public class Voucher
{
public int ID { get; set; }
public string Title { get; set; }
public string SiteName { get; set; }
public string DealURL { get; set; }
public DateTime AddedDate { get; set; }
public DateTime? ExpirationDate { get; set; }
public string VoucherFileURL { get; set; }
public Guid UserID { get; set; }
}
Here is what I have for Edit controller:
// POST: /Voucher/Edit/5
[HttpPost]
public ActionResult Edit([Bind(Exclude = "AddedDate")]Voucher voucher)
{
if (ModelState.IsValid)
{
db.Entry(voucher).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(voucher);
}
On Edit page, when I click on submit, I got the following error:
System.Data.SqlServerCe.SqlCeException: An overflow occurred while converting to datetime.
Seems like the AddedDate didn't get excluded from the voucher object and triggered the error.
Would you please let me know how to fix it? Thanks!
(it is an updated version of asp.net mvc3 UpdateModel exclude properties is not working, I will go with another approach)
Never use your domain entities as action arguments and never pass your domain entities to your views. I would recommend you to use view models. In the view model you will include only the properties that you want to be bound from the view. The view model is a class that's specifically tailored to the requirements of a given view.
public class VoucherViewModel
{
public int ID { get; set; }
public string Title { get; set; }
public string SiteName { get; set; }
public string DealURL { get; set; }
public DateTime? ExpirationDate { get; set; }
public string VoucherFileURL { get; set; }
}
and then:
[HttpPost]
public ActionResult Edit(VoucherViewModel model)
{
// TODO: if the view model is valid map it to a model
// and pass the model to your DAL
// To ease the mapping between your models and view models
// you could use a tool such as AutoMapper: http://automapper.org/
...
}
UPDATE:
In the comments section #Rick.Anderson-at-Microsoft.com points out that while I have answered your question I haven't explained where the problem comes from.
The thing is that DateTime is a value type meaning it will always have a value. The [Bind(Exclude = "AddedDate")] works perfectly fine and it does what it is supposed to do => it doesn't bind the AddedDate property from the request. As a consequence the property will have its default value which for a DateTime field is 1/1/0001 12:00:00 AM and when he attempts to save this in SQL Server it blows because SQL Server doesn't support such format.
Let's say you have an object called Person that looks like this:
class Person
{
public int ID { get; set; }
public string Name { get; set; }
public int NumberOfCatsNamedEnder { get; set; }
}
I have a simple HTML form that exposes the properties that gets posted to an ASP.NET MVC action inside of my PersonController class. The issue I have is that if someone puts in the letter 'A' for NumberOfCatsNamedEnder, I get a The model of type 'Person' was not successfully updated. error. Since this happens while trying to update the Model, I can't find any way to check to see if someone passed in a non-integer value without resorting to
if(!IsInteger(formCollection["NumberOfCatsNamedEnder"]))
{
ModelState.AddModelError(
"NumberOfCatsNamedEnder",
"Ender count should be a number");
}
Is there a better way to do this? I was able to find some information on custom ModelBinders; is that what is needed?
I really like the approach of using a presentation model. I'd create a class like this:
class PersonPresentation
{
public int ID { get; set; }
public string Name { get; set; }
public string NumberOfCatsNamedEnder { get; set; }
public void FromPerson(Person person){ /*Load data from person*/ }
}
Then your controller action can bind the view to a PersonPresentation:
public ActionResult Index()
{
Person person = GetPerson();
PersonPresentation presentation = new PersonPresentation();
ViewData.Model = presentation.FromPerson(person);
return View();
}
...and then accept one in your Update method and perform validation:
public ActionResult Update(PersonPresentation presentation)
{
if(!IsInteger(presentation.NumberOfCatsNamedEnder))
{
ModelState.AddModelError(
"NumberOfCatsNamedEnder",
"Ender count should be a number");
}
...
}