Lets say i've got simple ajax begin form:
#using (Ajax.BeginForm("DodajTresc", "Administracja", new AjaxOptions { Confirm = "Czy jesteś pewien że chcesz dodać treść ?", InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace, UpdateTargetId = "podgladDodanejTresc", LoadingElementId = "spinWiadomosci" }))
{
some code[...]
}
Is it possible to change UpdatetargetID after post depends of model state validation ?. For exmaple:
if(ModelState.IsValid)
{
UpdatetargetID="div1";
}
else
{
UpdatetargetID="div2";
}
Is there an option to achieved that ?
Related
I am trying to post to action the current querystring. So i have created the below extension method :
public static RouteValueDictionary ToRouteValues(this NameValueCollection col, Object obj = null)
{
var values = obj != null ? new RouteValueDictionary(obj) : new RouteValueDictionary();
if (col == null) return values;
foreach (string key in col)
{
//values passed in object are already in collection
if (!values.ContainsKey(key)) values[key] = col[key];
}
return values;
}
And in my view i am using the routvalues as below:
using (Ajax.BeginForm(actionName: "Post", routeValues:Request.QueryString.ToRouteValues(), controllerName: "Request", ajaxOptions: new AjaxOptions { HttpMethod = "Post", OnSuccess = "SuccessCallBack", UpdateTargetId = "successDiv", InsertionMode = InsertionMode.InsertAfter }, htmlAttributes: new { #data_toggle = "validator" }))
{
}
But strangely when the html markup is generated the form tag action does not have the actual querystring values but instead has a string version of the object metadata.
<form action="/Request/Post?Count=5&Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D" data-ajax="true" data-ajax-method="Post" data-ajax-mode="after" data-ajax-success="SuccessCallBack" data-ajax-update="#successDiv" data-toggle="validator" id="form0" method="post" novalidate="true">
This works for me:
#using (Ajax.BeginForm(
actionName: "AddOns",
controllerName: "Basket",
routeValues: Request.QueryString.ToRouteValues(),
ajaxOptions: new AjaxOptions { HttpMethod = "Post", OnSuccess = "SuccessCallBack", UpdateTargetId = "successDiv", InsertionMode = InsertionMode.InsertAfter },
htmlAttributes: new Dictionary<string, object> { { "class", "mainForm" } }))
{
#:Blah
}
It outputs:
<form action="/Basket/AddOns?test=test" class="mainForm" data-ajax="true" data-ajax-method="Post" data-ajax-mode="after" data-ajax-success="SuccessCallBack" data-ajax-update="#successDiv" id="form0" method="post">
Blah
</form>
Please note the last parameter which is a Dictionary<string, object>.
Used this post as source: MVC3 Html.BeginForm - passing arguments as RouteValueDictionary fails
The issue is that your using named parameters and there are overloads of Ajax.BeginForm() that accept both object routeValues and RouteValueDictionary routeValues.
In your case its using object routeValues and your generating a route value for each property of RouteValueDictionary (Count, Keys, etc). I'm assuming that's because the overload with object routeValues is defined first (but have yet to find anything documenting the behavior)
You can solve this by omitting the names and using
#using (Ajax.BeginForm("Post", "Request", Request.QueryString.ToRouteValues(), new AjaxOptions { HttpMethod = "Post", OnSuccess = "SuccessCallBack", UpdateTargetId = "successDiv", InsertionMode = InsertionMode.InsertAfter }, new { #data_toggle = "validator" }))
{
}
I've got pager like this:
#Html.PagedListPager(Model.komentarzeListModeracja, page =>
Url.Action("ModeracjaKomentarze", new {
DotyczyID = Model.DotyczyID, page
}),
PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions() {
HttpMethod = "Get", UpdateTargetId = "ModeracjaUpdate1"
}))
Could You tell me how can I add other render option to this ?. Is it possible ?.
There are already several versions\flavors incorporated into the class (intellisense enabled: Classic to OnlyShowFivePagesAtATime, Minimal and Bootstrap, etc.).
PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(PagedListRenderOptions.TwitterBootstrapPagerAligned, new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "divpagedlist" }))
PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(PagedListRenderOptions.ClassicPlusFirstAndLast, new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "divpagedlist" }))
Simply, comma separated properties for the class.
A good reference at this time for PagedListRenderOptions (Class) properties:
http://www.nudoq.org/#!/Packages/PagedList.Mvc/PagedList.Mvc/PagedListRenderOptions
(please note this site is beta, cannot be sure how long link will be valid)
Another way...
#Html.PagedListPager(Model, page => Url.Action("FileIndex",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }),
PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new PagedListRenderOptions()
{
DisplayLinkToLastPage = PagedListDisplayMode.IfNeeded,
DisplayLinkToFirstPage = PagedListDisplayMode.IfNeeded,
Display = PagedListDisplayMode.IfNeeded,
LiElementClasses = new List<string> { "myClass", "yourClass" },
MaximumPageNumbersToDisplay = 10
}, new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "fileListTable" }))
I want my Ajax.ActionLink to pass a viewModel property to action.
Here is my ViewModel
public class ViewModel
{
public string Searchtext { get; set; }
}
My .cshtml
#Ajax.ActionLink("Bottom3", "Bottom3",new { name = Model.Searchtext}, new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "pointsDiv"
})
using(Html.BeginForm("Bottom3", "Home", FormMethod.Get))
{
#Html.TextBoxFor(x => x.Searchtext)
<button type="submit">Search</button>
}
<div id="pointsDiv"></div>
}
My Controller action:
public PartialViewResult Bottom3(string name)
{
var model = db.XLBDataPoints.OrderBy(x => x.DataPointID).Take(3).ToList();
return PartialView("Partial1", model);
}
But the name parameter passed to the action is always null. How do I solve this?
In your code... you have 2 different ways of posting to the server: the link and the form button.
The problem is that the ActionLink has no way to get the value from the input in client side... just the original value.
If you press the Search button, you will see a value posted.
Now, you can use some jQuery to modify a standard ActionLink (not the Ajax.ActionLink):
https://stackoverflow.com/a/1148468/7720
Or... you can transform your Form in order to do a Ajax post instead of a normal one:
https://stackoverflow.com/a/9051612/7720
I did this for a model of mine like so. I ONLY supported the HttpPost method. So add the HttpMethod="POST" to your Ajax.ActionLink
[HttpPost]
public ActionResult Accounts(ParametricAccountsModel model)
{
if (model.Accounts == null)
{
GetAccountsForModel(model);
}
if (model.AccountIds == null)
{
model.AccountIds = new List<int>();
}
return View(model);
}
On the razor view
#Ajax.ActionLink(
"Add Account to Order", "Accounts", "Parametric", null,
new AjaxOptions() { InsertionMode = InsertionMode.Replace, UpdateTargetId = "...", HttpMethod = "POST" },
new { #id = "AddParametricAccountLink" })
The model has a list of selected account ids. So in javascript, I modified the href of the action link dynamically.
function UpdateParametricAccountAction() {
var originalLink = '/TradeNCashMgmt/Parametric/Accounts';
var append = '';
var numberOfRows = $('#ParametricAccounts').find('.parametric-account- row').size();
for (var i = 0; i < numberOfRows; i++) {
if (i != 0) {
append += '&';
}
else {
append = '?';
}
var idValue = $('#NotionalTransactionsAccountId_' + i).val();
append += 'AccountIds%5B' + i + '%5D=' + idValue;
}
$('#AddParametricAccountLink').attr('href', originalLink + append);
}
Since the model binder looks for parameter names in the query string and form submission, it will pick up values using the href. So I posted a model object using the querystring on my Ajax.ActionLink. Not the cleanest method, but it works.
I have a Ajax Action link, which will call a action Method,
In my Ajax Option i have called a Validate function,
If this function returns true,
then only i would want this Action Execute, not sure how i can get this done?
My Ajax ActionLink
Ajax.ActionLink("Renew", "Edit", "Controller", new { id = "<#= ID #>" },
new AjaxOptions
{
OnBegin = "isValidDate",
OnSuccess = "DestroyRecreateAccordion",
UpdateTargetId = "accordion",
InsertionMode = InsertionMode.InsertAfter,
}, new { #class = "standard button" })
How can I do this only if isValidDate returns true?
AjaxOptions on Action Link
OnBegin="isValidDate"
JavaScript
function isValidDate() {
var date = $('#dateid').val()'
//...check date....
if(date is valid) return true;
else return false;
}
this worked
You need to return false on your OnBegin Method
OnBegin = "function(){ return isValidDate(); }",
function isValidDate() {
var date = $('#dateid').val()'
...check date....
if(date is valid) return true;
else return false;
}
i have this code in View
#using (Html.BeginForm())
{
#: Location #Html.DropDownList("territryID", (SelectList)ViewBag.Territory, "choose one")
#Ajax.ActionLink(
"ok",
"Info", new { territryID = "#territryID" },
new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
UpdateTargetId = "post1"
})
<div id="post1">
</div>
}
and this code in my Controller
[HttpPost]
public ActionResult Info(int? territryID)
{
if (territryID == null)
{
return RedirectToAction("Info");
}
var model = (from c in _db.OCRDs
where c.Territory == territryID
select c).Distinct();
return PartialView("_getCustomerByTerritory", model);
}
how to passing my dropdownlist selected value to territryID parameter in controller, how to do that?
thanks,
erick
How about this, initialize your URL to something like this (also note the assignment of an id to the link):
#Ajax.ActionLink("ok", "Info", new { territryID = "REPLACEME" }, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "POST", UpdateTargetId = "post1" }, new { id = "oklink" })
Replace the placeholder (REPLACEME) when the dropdown changes, like so:
<script>
$('#territryID').change(function () {
var newid = $('#territryID').val();
var oldLink = $('#oklink').attr('href');
var newLink = oldLink.replace('REPLACEME', newid);
$('#oklink').attr('href', newLink);
});
</script>