has someone make ajax.actionlink for delete to work properly.After deleting the record successfully at the end it is not refreshing the page properly. the page refresh is my problem. i have defined the updatetarget id and returning view(model) from my controller but it is returning the master page with it.
So the thing is that i am having a page with a page.I have used redirect as well which is not refreshing,
<%= Ajax.ActionLink("Delete", "Delete", new { id = item.int_OrganizationGroupId }, new AjaxOptions { UpdateTargetId = "abc", HttpMethod = "Post", Confirm = "Delete Group with Organization Group ID:" + item.int_OrganizationGroupId + " Organization Group Name:" + item.vcr_OrganizationGroupName, OnSuccess = "handleSuccess" })%>|
abc is the id of the table
From controller
View("ManageGroup,Model);
To use AJAX returns on models and views, you need to create a partial view and return that instead of a full view which will include your master page, etc.
Public Function AjaxView() as ActionResult
Return PartialView("NameOfYourPartialView", ItemsToReturn)
End Function
Related
I have added a feature to add more users information by providing "add another user" link.
This link load a partial view on the page.And when I click save at the end after adding 3 0r 4 user partial view, How can i save this data into database ,
$("#addAnotherUserLink").click(function () {
$.ajax({
url: this.href,
cache: false,
success: function (html) {
$(".userDiv").append(" <div class='userPartialDiv'><a href='#' class='deleteRow'>Delete</a>" + html + "</div>");
}
});
return false;
});
My view derived from a viewmodel -- #model MyApp.DataModels.DemoViewModel
And controller
[HttpPost]
public ActionResult AddUserForm(DemoViewModel demoViewModel)
{
}
But user partial view is loaded dynamically. How to access that partial view model data into controller.
Thanks
Put Users (or DemoViewModel from your controller) view models into List (it can be only one viewmodel, another you will add by javascript) and print it in theview. It will generate inputs with indexes in the name and ID ... something like UsersModels_1_Property. When you are adding new row to your form, you have to to ensure that HTML inputs has right index on name and ID. In "create" action you will receave list of User view model.
Here is very good article about your problem http://www.techiesweb.net/asp-net-mvc3-dynamically-added-form-fields-model-binding/
It is necessary for me at a choice the user of a subject of a forum on the page from a database contents of the chosen subject were loaded on the page. I will describe how I approached to the solution of this task. This code presents forum threads:
<a href='Home/ThreadView' id='1'>About all</a>
<a href='Home/ThreadView' id='2'>Cars</a>
ThreadView action has code:
[HttpGet]
public ViewResult ThreadView(int id)
{
ViewData[ThreadViewSelector] = Thread.GetThreadView(id);//Thread is a model.
return View();
}
When I click on any link an error occurs:
Parameters dictionary contains record with value NULL for parameter "id" of type "System.Int32"
How to solve this problem ?
The ID attribute in your link is the id of the HTML element... so, that id could be useful if you want to do something with javascript.
With your current code, the id is never sent to the controller action, that is why the error you are seeing. You need to put the ID in the href attribute along with the Controller/Action.
You have 2 options to pass the id to your controller action:
If you have the out-of-the-box routing rules you can use:
<a href='Home/ThreadView/1' id='1'>About all</a>
You can send it via query string this way:
<a href='Home/ThreadView/?id=1' id='1'>About all</a>
EDIT:
The element id is not required in order to have the routing working.
Instead of doing this manually, you could use one of the Razor Helpers:
#Html.ActionLink("About all", "Home", "ThreadView", new { id = 1})
If you need to add some attributes to the link element, besides the routing parameters:
#Html.ActionLink("About all", "Home", "ThreadView", new { id = 1}, new { #class = "redLink", id="aboutall"})
I am using MVC3 with vb.net. I want to update the div value on button click. I know this can do by ajax call to action. but do i need to use partial view for that ? please show me example hint somewhere.
Edited:
I tried with ajax calling. My view is
<% Using (Html.BeginForm("Create", "mycontroller", FormMethod.Get, Nothing))%>
field 1....
field 2....
field 3....
<%Using (Ajax.BeginForm("Create", "DailyStatusReport", FormMethod.Get, New AjaxOptions With {.UpdateTargetId = "textEntered"}))%>
<span id="textEntered">field4...</span>
<Html Button to call ajax here on click >
<% End Using%>
<% End Using%>
while debugging it is executing the the value i wanted. but not rendering. what should be the problem. I tried cache clearing. I want "field4" should change after ajax call.
do i need to use partial view for that ?
No
#RedSwan - Ajax Calls never disturbs the page as mentioned in below example. Only specific part of the page gets updated.
Sample Ajax code
$.ajax({
url : "#Url.Action("ActionName", "ControllerName")",
contentType : "application/json; charset=utf-8",
dataType : "json",
type : "GET", //In case of non complex data only.
data : JSON.stringify({Param1:'test'})
}).done(function(Result) {
// Update the Div here
})
.fail(function() {
});
Hoping someone can help me solve this issue.
I'm using Ajax.BeginForm quite often when I need to update a part of my actual webpage. But if I have a second button in the web form where I need go to a complete different page, and for example do a search and get some values that I need to complete the form. The page turns up in the update panel (updateTargetID) instead of in a complete new View. That is happening even id a do a RedirectToAction in the controller. Of course the ajax.beginform does what I accept it to do, in other words update the panel that shall be updated. But is there a way to get ajax.Beginform to use redirectToaction without updating when the user is choosing to do for example a search instead of sending the form?
I'm working with asp.net MVC,
Problem;
<% using (Ajax.BeginForm(new AjaxOptions() { LoadingElementId = "loadingElement", UpdateTargetId = "SearchResult", InsertionMode = InsertionMode.Replace}))
{%>
<% Html.RenderPartial("Searchfields", Model); %>
<div>
<%:Html.ActionLink("blank", "Index")%>
</div>
<div id="loadingElement" style="display: none">
Load data...
</div>
<div id="SearchResult">
<% if (Model.SearchResult != null)
{
Html.RenderPartial("SearchResult", Model.SearchResult);
} %>
</div>
<% } %>
In the controller (post) I do this among other stuff;
if (Request.IsAjaxRequest())
{
return PartialView("SearchResult", data.SearchResult);
}
But before this I need to do:
if (data.SearchResult.Count() == 1)
{
return RedirectToAction("Edit", "Person", new { Id = data.SearchResult.First).Id});
}
but ofcourse if I do that the hole page i rendered in the corresponding updateTargetId,
I need to render a new view instead. So how can I for exampel use javascript to redirect oncomplete and have the values from the model sent to the script to for exampel do a window.location.replace?
I'm struggling with javascript, so please help me!
First, I use Asp.Net MVC 2 RC 2.
What I want to do is to list a comment view and below this view being able to add comment (with validations). For example, something like when you add comment in stackoverflow. Except that my page should work with or without javascript enabled.
So to solve that problem I use the new RenderAction and it partially solved my problem. I got my list view that calls my addcomment usercontrol with RenderAction.
The validations work. My problem occurs when I try to add a comment that it's valid. The page didn't refresh correctly. If I got in the database, my comment is added, but it's doesn't have been refresh in my list view and the add comment form isn't clear.
I think it's because of how the workflow is rendered.
Maybe if someone got a example or blog about this, it's can help me to get it right...
At the bottom of my Comment/List.aspx
<% Html.RenderAction("Create", "Comment"); %>
In Comment/Create.ascx
<% using (Html.BeginForm(
ViewContext.ParentActionViewContext.RouteData
.Values["action"].ToString(),
ViewContext.ParentActionViewContext.RouteData
.Values["controller"].ToString(),
FormMethod.Post, new { id = "createForm" })){ %>
You can force the parent View to refresh itself with a small hack involving ViewContext.ParentActionViewContext.
In your CommentController class:
public ActionResult Create(Comment comment)
{
...
if (isValid) // Comment entered in form is valid
{
ControllerContext.ParentActionViewContext.ViewData["SuccessfullCreate"] = true;
}
...
}
And in your Comment/List.aspx page (view) :
<% Html.RenderAction("Create", "Comment"); %>
<%
if (ViewContext.ViewData["SuccessfulCreate"] != null)
{
string action = ViewContext.RouteData.Values["action"].ToString();
string controller = ViewContext.RouteData.Values["controller"].ToString();
string url = "/" + controller + "/" + action;
Response.Redirect(url);
}
%>
So basically, what's happening is that the child action is "telling" the parent action to refresh itself by using the parent's ViewData.
It's kind of a hack, but it works fine for what's you're doing.