To address a particular requirement in ASP.net MC3, I wanted to use AjaxExtensions.RouteLink
But I wonder how to use it in a view.
For example:
#AjaxExtensions.RouteLink(ajaxHelper, link.Text, new { controller="Home",action="List",
category = id },
new AjaxOptions { UpdateTargetId = "itemList",InsertionMode=InsertionMode.Replace },
new { #class = "item" })
But while running it throws me error "The name 'ajaxHelper' does not exist in the current context". Since I am new to this usage,it stops me.Could anyone please share some details of using this one ? Thanks much
Used something like below instead:
#foreach (var link in Model)
{
if (link.SubCat.Count == 0)
{
#Ajax.RouteLink(link.Text, new
{
controller = "Home",
action = "List",
pId = link.Id,
parentCatId=link.ParentCatId
},
new AjaxOptions
{
UpdateTargetId = "itemList", //div name
InsertionMode = InsertionMode.Replace
},
new { #class = "menuitem" }
)
}
The above code is in a PartialView. But now the problem with this is , it doesn't update the result to the Target provided. Instead it replaces the result to the whole page. Hopefully somebody now can give me a clue to get rid of this.
Related
Is there any way to make #Ajax.ActionLink() not update UpdateTargetId?
For example, with something like this:
#Ajax.ActionLink("All", "Get_Books_By_Id", new { ID = 0 },
new AjaxOptions() {
HttpMethod = "GET",
UpdateTargetId = "div_records",
InsertionMode = InsertionMode.Replace,
LoadingElementId = "div_load"
}
)
Get_Books_By_Id is returning PartialView with data for table in PartialView, but I would like to not update UpdateTargetId when data (List) count is 0, for example.
Is there any way to send via control something what would tell ajax not to update? Or is there any other way?
You can do it in two ways:
Remove the update target id and instead use the OnSuccess handler
function onSuccess(result){
var fragment = $(result);
if(fragment.find("tr").length >= 0)
$("div_records").html(fragment)
}
Change your controller to return a different status code from your action
public ActionResult Get_books_by_id()
{
var myBooks = _repostiory.GetBooksById();
if(!myBooks.Any())
return new HttpStatusCodeResult(HttpStatusCode.NotFound);
return PartialView("mybooks", myBooks);
}
I've used both methods before, and obviously you will need to tailor the selects and actions to your use case.
I have a checkbox like this with defined value and text.
#Html.CheckBoxFor(model => model.UserGroup.administration, new[] { new SelectListItem { Text = "True", Value = "1" }, new SelectListItem { Text = "False", Value = "0" } })
I would like to add another html attributes which is "id=1", but it gave me an error if I attempt to do so in new[] {...}
I get the following errors : “No best type found for implicitly-typed array”
Any idea?
If you want to manually set the control ID to MyClass you can do it like this:
#Html.CheckBoxFor(model => model.UserGroup.administration, new { id = "MyClass" })
The other attributes should be mapped automatically if your model.UserGroup.administration is a boolean...
i have the following Ajax.actionlink inside a view to add an answer under a question:-
#Ajax.ActionLink("Add Answers",
"Create", "Answer",
new { questionid = question.QuestionID},
new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
HttpMethod = "Get",
UpdateTargetId = "removetable"
})
while will call the following action method:-
public ActionResult Create(int questionid)
{
ViewBag.IsRight = new SelectList(repository.FindAllAnswerDescription().ToLis(), "IsRight", "description", 1);
ViewBag.questionid = questionid;
Answer answer = new Answer();
return PartialView("_answer",answer); }
so my question is will a hacker be able to modify the new { questionid = question.QuestionID}, parameter send by the ajax link ? and if yes how i can avoid this.
BR
Edited:-
i am doing the following check using a helper method (IsauthorizedBy) on the post action method to check if the user is authorized to answer a question or not:-
[HttpPost]
public ActionResult Create(int questionid, Answer a)
{
q = repository.findquestion(questionid);
if ((q == null) || (!q.IsauthorizedBy(User.Identity.Name))){
return ("error");}
if (ModelState.IsValid)
{
repository.AddAnswer(a);
repository.Save();
return PartialView("_details",a);
}
return(a);}
so will it handel a hacker who will try to modify the question id and answer a question he is not authorized to answer.
BR
Yes but you want to ensure on the server side they have access to this question by querying their permissions and some database scheme in place ensuring they have access to this. If it's not feasible then you can use
Html.AntiModelInjectionFor from
mvcsecurity.codeplex.com plus
[ValidateAntiModelInjection()]
You never trust anything coming from the client-side. Everything can be altered on the way, either in the scripts or on the network.
Since it's only an id, you need to be careful and do extra checks on the server
I'm trying to setup a routing scheme in MVC3 that matches against a legacy (SP 2007) system. These are the routes I've setup:
routes.MapRoute("administration",
"Administration/{action}/{id}",
new { controller = "Administration", action = "Index", id = UrlParameter.Optional });
routes.MapRoute("workOrderSearch",
"WorkOrderSearch",
new {controller = "Home", action = "WorkOrderSearch"});
routes.MapRoute("customers",
"{customerNumber}/{action}",
new {controller = "customer", action = "Index"},
new {customerNumber = #"\d*"});
routes.MapRoute("graphicNames",
"{customerNumber}/{graphicNameId}/{action}/{id}",
new {controller="GraphicName", action="Index", id=UrlParameter.Optional},
new {customerNumber = #"\d*",graphicNameId = #"\d*", action=#"\w*"});
routes.MapRoute("workOrders",
"{customerNumber}/{graphicNameId}/{graphicNumber}/WorkOrder/{action}/{id}",
new { controller = "WorkOrder", action = "Index", id = UrlParameter.Optional },
new { graphicNameId = #"\d*", graphicNumber = #"\d*-\d*" });
routes.MapRoute("graphics",
"{customerNumber}/{graphicNameId}/{graphicNumber}/{action}",
new { controller = "Graphic", action = "Index", id = UrlParameter.Optional },
new { graphicNameId = #"\d*", graphicNumber = #"\d*-\d*" });
routes.MapRoute("Default", "", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
It mostly works just fine. However, when trying to hit the "graphicNames" route, I run into a problem. If I use this url:
http://localhost:1234/1234/321/Index
it works fine and I get to the Index action on the GraphicName controller. However, if I do this:
http://localhost:1234/1234/321
I get a 404.
All other routes appear to work as expected.
Edit: The solution was to add a constraint to the customer's route so that actions were only 'action=#"[A-Za-z]*"
Above you have:
routes.MapRoute("graphicNames",
"{customerNumber}/{graphicNameId}/{action}/{id}",
new {controller="GraphicName", action="Index", id=UrlParameter.Optional},
new {customerNumber = #"\d*",graphicNameId = #"\d*", action=#"\w*"});
however before that you have
routes.MapRoute("customers",
"{customerNumber}/{action}",
new {controller = "customer", action = "Index"},
new {customerNumber = #"\d*"});
which based on your url with only TWO parameters
/1234/321
will match the customers route first. Either add a route constraint that action must be alpha only, or move this beneath your graphicNames route, since order is very important in route matching.
I have an area called MyArea and it's registered like so:
context.MapRoute(null, "MyArea", new { controller = "MyAreaController", action = "Index" });
//Properties
context.MapRoute(null, "MyArea/properties", new { controller = "Property", action = "Index" });
context.MapRoute(null, "MyArea/properties/edit/{propertyId}", new { controller = "Property", action = "Property" });
//Units
context.MapRoute(null, "MyArea/properties/edit/{propertyId}/units/{unitId}", new { action = "Unit", propertyId = 1, unitId = 1 });
It should work that one property has many units, so I would like my url to look something like this:
http://localhost:50182/myarea/properties/edit/4/units/1
The code i use for the Html.ActionLink looks like:
#Html.ActionLink("Add new Unit", "Unit", "Unit", new { propertyId = 1, unitId = 1 })
I have an Unit controller with an action called Unit. Pleas help, what am i missing?
Thanks!!
You say "I have an Unit controller with an action called Unit. Pleas help, what am i missing?"
and your route mapping is currently ...
context.MapRoute(null, "MyArea/properties/edit/{propertyId}/units/{unitId}", new { action = "Unit", propertyId = 1, unitId = 1 });
How would you expect MVC to know what controller to use for that route? You need to specify controller = "Unit"
Update
Switch the order of
context.MapRoute(null, "MyArea/properties/edit/{propertyId}", new { controller = "Property", action = "Property" });
//Units
context.MapRoute(null, "MyArea/properties/edit/{propertyId}/units/{unitId}", new { action = "Unit", propertyId = 1, unitId = 1 });
in your route registration. Otherwise, something that should map to the second route will be intercepted by the first.