Action name containing a question mark? - asp.net-mvc-3

Using asp.net mvc 3.0 what would i have to do to provide the following route
public class ProductController : Controller
{
// ************************
// URL : Product/Create
// ************************
public ActionResult Create()
{
return View();
}
// ************************
// URL : Product/Create?Page=Details
// ************************
[ActionName("Create?Page=Details")]
public ActionResult CreateDetails()
{
return View();
}
}
Thanks
Rohan

public class QueryStringConstraint : IRouteConstraint
{
public QueryStringConstraint(string value, bool ignoreCase = true)
{
Value = value;
IgnoreCase = ignoreCase;
}
public string Value { get; private set; }
public bool IgnoreCase { get; private set; }
public virtual bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
var currentValue = httpContext.Request.QueryString[parameterName];
return IgnoreCase ? currentValue.ToLowerInvariant() == Value.ToLowerInvariant() : currentValue == Value;
}
}
routes.MapRoute("Create page details", "Product/Create",
new { controller = "Product", action = "CreateDetails" },
new { page = new QueryStringConstraint("details") });
Alternatively if you have different models for those actions, you could do something like this (with standard "{controller}/{action}/{optional id}" route):
public class RequireRequestValueAttribute : ActionMethodSelectorAttribute
{
public RequireRequestValueAttribute(string name, string value = null, bool ignoreCase = true)
{
Name = name;
Value = value;
IgnoreCase = ignoreCase;
}
public string Name { get; private set; }
public string Value { get; private set; }
public bool IgnoreCase { get; private set; }
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
var value = controllerContext.HttpContext.Request[Name];
return value != null && (Value == null || (IgnoreCase ? Value.ToLowerInvariant() == value.ToLowerInvariant() : Value == value));
}
}
[RequireRequestValue("Page", "Detail")]
public ActionResult Create(ProductDetailModel model)
{
return View(model);
}
[RequireRequestValue("Page", "Overview")]
public ActionResult Create(ProductOverviewModel model)
{
return View(model);
}

An action name cannot contain a question mark. The question mark is a reserved character in a URL indicating the beginning of the query string.

What if you not create another action? Just call your "Create" action with the query string.
http://localhost/Home/Create?Page=Details
public ActionResult Create()
{
var page = Request.QueryString["Page"];
// do your stuff, or redirect here if you like
// return RedirectToAction("Create" + page);
return View();
}

Related

how to apply generic multiple include with generic repository?

I need to make generic multiple include function as a service with generic repository.
But unfortunately, I get nothing !!
here is my attempt using aggregate linq.
public IQueryable<TEntityDTO> getRowsWithIncludeMultiple(int page = 0, params Expression<Func<TEntityDTO, object>>[] includes)
{
GridSetting gs = GetGrid();
IEnumerable<TEntity> getPage = _dbSet.Skip((page == 0 ? page : page - 1) * gs.ItemsPerPage).Take(gs.ItemsPerPage);
IQueryable<TEntityDTO> rows = _mapper.Map<IEnumerable<TEntityDTO>>(getPage).AsQueryable();
if (includes != null) { rows = includes.Aggregate(rows, (current, include) => current.Include(include)); }
// or
//foreach (var include in includes)
//{
// rows = rows.Include(include);
//}
return rows;
}
when I add debugging point I get that the includes has list of expression
and then here is how I use it
var xxx = _customerService.getRowsWithIncludeMultiple(page: 0, i => i.cityDTO, i => i.ageDTO);
the problem here I get customers without the included things (cityDTO & ageDTO)
let me include here models
public class CustomerDTO
{
public int Id { get; set; }
public string CustName { get; set; }
public string CustJobTitle { get; set; }
public string CustAge { get; set; }
public bool IsManager { get; set; }
// FKs
public int AgeId { get; set; }
public int CityId { get; set; }
public AgeDTO ageDTO { get; set; }
public CityDTO cityDTO { get; set; }
}
public class CityDTO
{
public int Id { get; set; }
public string CityName { get; set; }
public List<CustomerDTO> customerDTO { get; set; }
}
public class AgeDTO
{
public int Id { get; set; }
public int AgeName { get; set; }
public List<CustomerDTO> customerDTO { get; set; }
}
Update ... showing the whole service, usage, and injection
here is the whole generic repository service and how it looks like
public class Repository<TEntity, TEntityDTO> : IRepository<TEntity, TEntityDTO> where TEntity : class where TEntityDTO : class
{
protected readonly AppDbContext _context;
private readonly DbSet<TEntity> _dbSet;
private readonly IMapper _mapper;
public Repository(AppDbContext context, IMapper mapper)
{
_context = context;
_dbSet = context.Set<TEntity>();
_mapper = mapper;
}
// GENERIC CRUD ...
// and then here where i want to focus
public IQueryable<TEntityDTO> getRowsWithIncludeMultiple(int page = 0, params Expression<Func<TEntityDTO, object>>[] includes)
{
GridSetting gs = GetGrid();
IEnumerable<TEntity> getPage = _dbSet.Skip((page == 0 ? page : page - 1) * gs.ItemsPerPage).Take(gs.ItemsPerPage);
IQueryable<TEntityDTO> rows = _mapper.Map<IEnumerable<TEntityDTO>>(getPage).AsQueryable();
if (includes != null) { rows = includes.Aggregate(rows, (current, include) => current.Include(include)); }
// or
//foreach (var include in includes)
//{
// rows = rows.Include(include);
//}
return rows;
}
}
and then here is how customer service uses generic repo
public class CustomerService : Repository<Customer, CustomerDTO>, ICustomerService
{
public CustomerService(AppDbContext db, IMapper mapper) : base(db, mapper) { }
}
finally injection in Program.cs
builder.Services.AddScoped(typeof(IRepository<,>), typeof(Repository<,>));
builder.Services.AddScoped<ICustomerService, CustomerService>();
thanks #Svyatoslav Danyliv and thanks for all contributors your answer helped me a lot.
cannot apply Include to DTO object
so, I would to share what I have made so far.
instead of
public IQueryable<TEntityDTO> getRowsWithIncludeMultiple(int page = 0, params Expression<Func<TEntityDTO, object>>[] includes)
{
GridSetting gs = GetGrid();
IEnumerable<TEntity> getPage = _dbSet.Skip((page == 0 ? page : page - 1) * gs.ItemsPerPage).Take(gs.ItemsPerPage);
IQueryable<TEntityDTO> rows = _mapper.Map<IEnumerable<TEntityDTO>>(getPage).AsQueryable();
if (includes != null) { rows = includes.Aggregate(rows, (current, include) => current.Include(include)); }
return rows;
}
I applied include to the original entity not to DTO
public IEnumerable<TEntityDTO> IncludeMultiple(int page = 0, params Expression<Func<TEntity, object>>[] includes)
{
GridSetting gs = GetGrid();
IQueryable<TEntity> rows = _dbSet.Skip((page == 0 ? page : page - 1) * gs.ItemsPerPage).Take(gs.ItemsPerPage).AsQueryable();
if (includes != null)
{ rows = includes.Aggregate(rows, (current, include) => current.Include(include).AsQueryable().AsNoTracking()); }
return _mapper.Map<IEnumerable<TEntityDTO>>(rows).AsQueryable();
}
And here is some clarification to make the idea more understandable
public IEnumerable<TEntityDTO> IncludeMultiple(params Expression<Func<TEntity, object>>[] includes)
{
// get IQueryable of TEntity
IQueryable<TEntity> rows = _dbSet.AsQueryable();
// and here using `linq Aggregate` to append multiple include statement
if (includes != null)
{ rows = includes.Aggregate(rows, (current, include) => current.Include(include).AsQueryable().AsNoTracking()); }
// here I use auto mapper to map IEnumerable object to IEnumerable object as a result
return _mapper.Map<IEnumerable<TEntityDTO>>(rows).AsQueryable();
}
and finally the usage
_ServiceName.IncludeMultiple(i => i.city, i => i.age);

Web API Parameter Binding

Request Payload does not get coverted to Custom Request Object.
payload
appl5=MC~IC&i~PhoneToPhone~inet_ptt_cb_phn~1=440&inet_ptt_cb_phn~3=7406&i~PhoneToPhone~inet_ptt_cb_delay=0&BeenHere=TRUE
It has ~ in keyvalue pair (both in key and value).
I have a Request Model that convert the input params to avalid Object.
Note: I cannot have ~ in my C# property. ( Can i ? )
My Post method has the following code
public HttpResponseMessage Post(ClientRequest request)
{
HttpResponseMessage response;
try
{
ProcessRequest target = new ProcessRequest(myRepository, myService);
response = target.Process(request);
}
catch (Exception exception)
{
response = Request.CreateErrorResponse(HttpStatusCode.NotFound, exception.Message);
//TODO : Log Exception.
}
return response;
}
Model
public class ClientRequest
{
public string Appl5 { get; set; }
public string I_PhoneToPhone_inet_ptt_cb_phn_1 { get; set; }
public string I_PhoneToPhone_inet_ptt_cb_delay { get; set; }
public string Inet_ptt_cb_phn_3 { get; set; }
public string BeenHere { get; set; }
}
My request object does not have the values for i~PhoneToPhone~inet_ptt_cb_phn~1, its null.
My understanding was the model binding is not happening because the payload key does not match
with my model (ClientRequest) which does not have ~ for i~PhoneToPhone~inet_ptt_cb_phn~1
in stead i have i_PhoneToPhone_inet_ptt_cb_phn_1
Should i use Custom Binding ?
At last, Added Custom Model binder
public class PostParameterModelBinder : IModelBinder
{
bool IModelBinder.BindModel(System.Web.Http.Controllers.HttpActionContext actionContext, ModelBindingContext bindingContext)
{
bool success = false;
if (bindingContext.ModelType == typeof(ClientRequest))
{
NameValueCollection postData = null;
postData = actionContext.Request.Content.ReadAsFormDataAsync().Result;
ClientRequest clientrequest = MapPostDataToRequest(postData);
bindingContext.Model = clientrequest;
success = true;
}
return success;
}
}
}

knockoutjs with mvc collection model binding

I'm using knockoutjs to render a collection of items. After allowing the user to do some inline editing I need to post the collection back to the server. However, the collection isn't being populated on the server because I'm not using the name="[0].Blah" naming convention. Does anyone know how to either render name attributes like this using knockoutjs OR how to create a model binder that will allow me to extract the values from the ValueProvider?
You can see a screenshot of the ValueProvider during debugging below.
http://i.imgur.com/zSU5Z.png
Here is my managed ViewModel:
public class FundLevelInvestmentUploadResult
{
public string FileName { get; set; }
public IList<FundLevelInvestmentViewModel> Items { get; set; }
public int NumberOfErrors { get; set; }
public bool ShowErrorsOnly { get; set; }
public FundLevelInvestmentUploadResult()
{
Items = new List<FundLevelInvestmentViewModel>();
}
}
Here is the managed class for "Items":
public class FundLevelInvestmentViewModel
{
private string _fund;
private string _fundType;
private string _date;
private string _netOfWaivedFees;
private string _waivedFees;
private string _bcip;
private string _fxRate;
public uint RowIndex { get; set; }
public int? DealCode { get; set; }
public bool DealCodeIsValid { get; set; }
public string Fund
{
get { return _fund; }
set { _fund = GetString(value); }
}
public bool FundIsValid { get; set; }
public string FundType
{
get { return _fundType; }
set { _fundType = GetString(value); }
}
public bool FundTypeIsValid { get; set; }
public string DateOfInvestment
{
get { return _date; }
set { _date = GetString(value); }
}
public bool DateOfInvestmentIsValid { get; set; }
public string NetOfWaivedFees
{
get { return _netOfWaivedFees; }
set { _netOfWaivedFees = GetString(value); }
}
public bool NetOfWaivedFeesIsValid { get; set; }
public string WaivedFee
{
get { return _waivedFees; }
set { _waivedFees = GetString(value); }
}
public bool WaivedFeeIsValid { get; set; }
public string BCIP
{
get { return _bcip; }
set { _bcip = GetString(value); }
}
public bool BCIPIsValid { get; set; }
public string ExchangeRateToUSD
{
get { return _fxRate; }
set { _fxRate = GetString(value); }
}
public bool ExchangeRateToUSDIsValid { get; set; }
public string FileName { get; set; }
private IList<string> _errors;
public IList<string> Errors
{
get { return _errors ?? (_errors = new List<string>());}
set { _errors = value; }
}
public bool Show { get; set; }
public FundLevelInvestmentViewModel()
{
Errors = new List<string>();
Show = true;
}
// knockoutjs is returning "null" instead of "" for a null object when calling ko.mapping.fromJS
private string GetString(string value)
{
if (value == "null")
return string.Empty;
return value;
}
}
Here is my knockout viewModel:
var viewModel = {
FileData: ko.observableArray([]),
validateFile: function (file, event) {
$.ajax({
type: 'post',
url: newUrl,
data: ko.mapping.toJS(file)
}).done(function (data) {
var newFile = ko.mapping.fromJS(data);
var index = file.Items.indexOf(file);
viewModel.FileData.replace(file, newFile);
});
}
};
If you are using version 2.1.0.0 or later of knockout you can render the name attribute as follows from an observable array.
<input data-bind='attr: { name: "Items["+$index()+"].DealCode"}' />

How to model bind a class that implements an interface?

The model binding worked fine until i implemented interfaces on top of the following classes:
public class QuestionAnswer : IQuestionAnswer
{
public Int32 Row_ID { get; set; }
public Int32 Column_ID { get; set; }
public String Value { get; set; }
}
public class HiddenAnswer : IHiddenAnswer
{
public Int32 Hidden_Field_ID { get; set; }
public String Hidden_Field_Value { get; set; }
}
public class SurveyAnswer : ISurveyAnswer
{
public string SessionID { get; set; }
public List<IQuestionAnswer> QuestionAnswerList { get; set; }
public List<IHiddenAnswer> HiddenAnswerList { get; set; }
public SurveyAnswer()
{
QuestionAnswerList = new List<IQuestionAnswer>();
HiddenAnswerList = new List<IHiddenAnswer>();
}
}
Now that the interfaces are there, i get a 500 (Internal Server Error)
The javascript that i use to model bind is the following:
$('#submitbutton').click(function () {
var answers = new Array();
var hiddenfields = new Array();
var formname = "#" + $("#formname").val();
$(':input', formname).each(function () {
if ($(this).is(":text") || $(this).is(":radio") || $(this).is(":checkbox"))
{
var answerObject = {
Column_ID: $(this).attr('data-column_id'),
Row_ID: $(this).attr('data-row_id'),
Value: $(this).attr('data-theValue')
};
answers.push(answerObject);
}
else if($(this).is(":hidden")) {
var hiddenObject =
{
Hidden_Field_ID: $(this).attr('data-hidden_field_id'),
Hidden_Field_Value: $(this).attr('data-hidden_field_value')
}
hiddenfields.push(hiddenObject);
}
});
$('textarea', formname).each(function () {
var answerObject = {
Column_ID: $(this).attr('data-column_id'),
Row_ID: $(this).attr('data-row_id'),
Value: $(this).val(),
};
answers.push(answerObject);
});
var allAnswers = {
SessionID: 0,
QuestionAnswerList: answers,
HiddenAnswerList: hiddenfields
}
postForm(allAnswers);
});
The Controller Action looks like this:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SubmitSurvey(SurveyAnswer answers)
{
// Dette tillader CORS
Response.AppendHeader("Access-Control-Allow-Origin", "*");
bc.SaveSurvey(answers);
return null;
}
what am i doing wrong?
what am i doing wrong?
You cannot expect the model binder to know that when it encounters the IQuestionAnswer interface on your SurveyAnswer view model it should use the QuestionAnswer type. It's nice that you have declared this implementation of the interface but the model binder has no clue about it.
So you will have to write a custom model binder for the IQuestionAnswer interface (same for the IHiddenAnswer interface) and indicate which implementation do you want to be used:
public class QuestionAnswerModelBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
var type = typeof(QuestionAnswer);
var model = Activator.CreateInstance(type);
bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, type);
return model;
}
}
which will be registered in your Application_Start:
ModelBinders.Binders.Add(typeof(IQuestionAnswer), new QuestionAnswerModelBinder());

Custom model binder for inner model

I got a model like this:
public class MainModel
{
public string Id {get;set;}
public string Title {get;set;}
public TimePicker TimePickerField {get;set;}
}
TimePicker is an inner model which looks like this:
public class TimePicker
{
public TimeSpan {get;set;}
public AmPmEnum AmPm {get;set;}
}
I'm trying to create a custom model binding for inner model: TimePicker
The question is: How do I get values in custom model binder which was submitted in form into TimePicker model fields?
If I try to get it like this:
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
I just get null in value.
I'm not sure how to implement the model binder correctly.
public class TimePickerModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException("bindingContext");
}
var result = new TimePicker();
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (value != null)
{
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, value);
try
{
//result = Duration.Parse(value.AttemptedValue);
}
catch (Exception ex)
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex.Message);
}
}
return result;
}
}
The following works for me.
Model:
public enum AmPmEnum
{
Am,
Pm
}
public class TimePicker
{
public TimeSpan Time { get; set; }
public AmPmEnum AmPm { get; set; }
}
public class MainModel
{
public TimePicker TimePickerField { get; set; }
}
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new MainModel
{
TimePickerField = new TimePicker
{
Time = TimeSpan.FromHours(1),
AmPm = AmPmEnum.Pm
}
};
return View(model);
}
[HttpPost]
public ActionResult Index(MainModel model)
{
return View(model);
}
}
View (~/Views/Home/Index.cshtml):
#model MainModel
#using (Html.BeginForm())
{
#Html.EditorFor(x => x.TimePickerField)
<button type="submit">OK</button>
}
Custom editor template (~/Views/Shared/EditorTemplates/TimePicker.cshtml) which merges the Time and AmPm properties into a single input field and which will require a custom model binder later in order to split them when the form is submitted:
#model TimePicker
#Html.TextBox("_picker_", string.Format("{0} {1}", Model.Time, Model.AmPm))
and the model binder:
public class TimePickerModelBinder : IModelBinder
{
public object BindModel(
ControllerContext controllerContext,
ModelBindingContext bindingContext
)
{
var key = bindingContext.ModelName + "._picker_";
var value = bindingContext.ValueProvider.GetValue(key);
if (value == null)
{
return null;
}
var result = new TimePicker();
try
{
// TODO: instead of hardcoding do your parsing
// from value.AttemptedValue which will contain the string
// that was entered by the user
return new TimePicker
{
Time = TimeSpan.FromHours(2),
AmPm = AmPmEnum.Pm
};
}
catch (Exception ex)
{
bindingContext.ModelState.AddModelError(
bindingContext.ModelName,
ex.Message
);
// This is important in order to preserve the original user
// input in case of error when redisplaying the view
bindingContext.ModelState.SetModelValue(key, value);
}
return result;
}
}
and finally register your model binder in Application_Start:
ModelBinders.Binders.Add(typeof(TimePicker), new TimePickerModelBinder());

Resources