Strait to the details...
I'm working on a personal project basically it's a task list. Anyways; I managed to get the standard Add, Edit, Delete a task functionality going good; now I'm stuck on something that I know must be very simple. I would like for users to be able to accept a task from the details page, now I could easily put that option in a drop down list and allow the user to select it and then save; BUT i would like to just provide a link that they can click "Accept Task" that link then goes to my controller action and pulls the task then updates the TaskStatus field.
This is my controller action
//
// TaskStatus Updates
[AcceptVerbs(HttpVerbs.Post), Authorize]
public ActionResult AcceptTask(int id)
{
Task task = taskRepository.GetTask(id);
try
{
task.TaskStatus = "Accepted";
taskRepository.Save();
return RedirectToAction("Details", new { id = task.TaskId });
}
catch (Exception)
{
throw;
}
}
So now how do I call this action from within my "Details" view?
A small form containing a button that posts back to the AcceptTask action would work. It could even be an AJAX form, if you wanted.
<% using (Html.BeginForm("accepttask","task",new { id = Model.TaskId })) { %>
<input type="submit" value="Accept Task" />
<% } %>
A link (<a>) cannot post a form, so you have three choices:
Use a button instead;
Use an <input type="image"> instead, which you can finesse to look similar to a link;
Use JavaScript to do it.
There's already an answer posted for #1, which is easily modified to #2. I'll post a jQuery example for #3, although you can use any JS library for this:
<a id="acceptTaskLink" href="#">Accept</a>
<span id="accepted" style="display: none">Accepted</span>
...
<script type="text/javascript">
$('#acceptTaskLink').click(function() {
$.post('/site/task/accept',
function(result) {
$('#acceptTaskLink').hide();
$('#accepted').show();
});
});
</script>
This script makes an Ajax post to the controller, then after it successfully posts, it changes the "Accept" link into regular "Accepted" text (by hiding the link and showing a confirmation element, which is presumably in the same place).
A link should not perform any action by definition. It leads to strange problems: What happens when google crawls your site? What happens when somebody refreshes the page? What happens when somebody bookmarks the page?
Actions that change content are usuallly done by get after post. First post to the Action that changes something (AcceptTask) then redirect to a page that displays the result. In you case I would suggest for the get the list of tasks with a success message. For the Mesage to be available after redirect use Tempdata.
Related
this goal page
as in the link above
I want to see the sub-object of the tree on the web, so if I press the ▶ button, I want to search the DB through efcore.
To solve this, I thought about how to execute a specific function in cshtml, but I realized that the problem is that even if the function is executed, there is no change in the view.
Whenever the ▶ button to view sub-items in the tree is clicked, I want to search for sub-items in the DB and update the view without page redirection.
I tried using the redirect method. it is not comfortable
cshtml
#if (Model.authority)
{content }
cshtml.cs
public IActionResult OnPostChagneMainCategorySelect()
{
return RedirectToPage("/MyPage/Leader", new
{
_selectionWorkState= workState,
_selectionItem = searchingItems,
_selectionMain = searchingCategorys,
});
}
If you want to return the same page you are on you can use
return Page();
I am trying to call a controller method which will generate from a cshtml page. While the controller is performing the action, the button must remain disbled and must only be enabled after the entire process is complete. Now the button gets disabled but it does not enable after the process is complete. Also if you refresh the page, even if the process is still going on the button gets enabled which should not happen Is there any help on how I can acheive this?
.cshtml file
function generate()
{
$('generate').attr('disabled',true);
alert('Process started')
var date=$(#ReportFromDate).val();
$ajax({
url:#Url.Content("Home/GenerateReport"),
type:"POST",
data:{ReportFromDate:date},
traditional:true,
success:function(response) {
//alert()
}
});
};
this is where I am calling the above ajax call
<input name="generate" id="generate" type="button" value="generate" onclick="generate">
Controller file
[HttpGet]
public ActionResult Report()
{
var reportModel= new ReportModel;
return View("Report",reportModel)
}
[HttpPost]
public ActionResult GenerateReport()
{
this.repository.generateReport(model);
return View("Report");
}
Problem is you disable button before sending request, but don't enable it after finishing ajax request. To do so, you should use $('generate').removeAttr('disabled') inside success and also error event of your ajax request.
Note: Also $('generate') is not correct and you should access element with class with . prefix or id with # prefix.
I have to call post action method in controller. my razor view is
#using (Html.BeginForm())
{
#Html.TextBox("count")
// here i have many controls
<input type="submit" value="SUBMIT"/>
}
[HttpPost]
public ActionResult Update(string count)
{
// i will do many business related actions
return View();
}
My question is, can i use normal post call by clicking submit button or shall i use ajax post method?
Which method of calling is good in mvc3 and why?
You must know the difference between Normal post and ajax
if you dont want to refresh your page then use ajax i.e you have to update content dynamically and stay on that page , otherwise use normal post
Both methods are fully supported, and the choice depends on your scenario.
You may use Ajax post method if you don't want to reload all your page on submit. Ajax allows you to make some partial refresh, so you refresh only a part of your page WHILE the normal post call loads an entire page(including headers and some other content that may not be changing).
garethb has given an answer in this link http://www.codeproject.com/Articles/429164/Html-BeginForm-vs-Ajax-BeginForm-in-MVC3.
the answer is
Ajax forms are suitable in situations, where you need to do modify or save operations asynchronously , without redirecting to any other forms.
You are using the MVC of ASP.NET, which is server-side. Posts are issued by the Javascript written on the client-side. Posts are supported by your server-side, so you can use "normal" or ajax posts as you like.
i have a list of thumbnails for the user can select one of the image.
onclick on the thumbnail open a larger image into a form.
What im trying to do now is send the id of the image selected to my controller.
Note: im using MVC 4.
how can i do that?
someone can help with this pls?
Thanks in advance:
Here is my code:
#foreach (var p in ViewBag.Images)
{
<li>
<a href="~/Files/#p.Name" onclick="swap(this); return false;">
<img src="~/Files/#p.Name"/>
</a>
</li>
}
when selected is going this img tag in my form:
<img id="main" src="" >
using this javascript for this event:
function swap(image) {
document.getElementById("main").src = image.href;
}
what i have to do now?
i trying with <input type="hidden" name="Img_Id" value="Viewbag.??????"/>
to pass this value to my controller??
First, some terminology help: You can't pass a value from the view to the controller action, the view is rendered after the controller action completes.
What you want to do is pass data from the client (web browser) to a controller action, using form fields.
In your javascript swap method, you could set the value of the Img_Id field to be the value for the selected image. When the form is submitted, the Img_Id will be posted as form data, and can be accepted as a parameter in the action.
You can use JQuery (or something else) to perform the client side actions.
Here's an example (not tested though!):
First add the ID as a data attribute on the element:
<a href="~/Files/#p.Name" data-id="#p.ID" onclick="swap(this); return false;">
Then some javascript to save that to form (using jquery here):
function swap(image) {
document.getElementById("main").src = image.href;
$("input[name='Img_Id']").val($(image).data("id"));
}
To pass a value back to your controller, you either need to submit a form, or else make an AJAX request to your controller.
In the first case, you'd need to update the value of your hidden field with javascript, and then either wait for the user to submit the form, or trigger a submit through javascript depending on what your needs are.
If you want to do an ajax request, it would be more or less the same thing, but you don't need a hidden field to store the value.
You could use jQuery in your swap function. See here for the official documentation.
If you chose to use this approach, and assuming you place your JavaScript in a separate file, then make sure you get the path for the action and controller and pass that in too.
var url = #Url.Action("Index","Home");
Therefore you may call: onclick="swap(this.id, url)"
I am trying to redirect to a post action result from another action result function. In this case, I would like to redirect to the Index Post, from the Summary function. Is that possible?
The index page is my search page and the Post action would return results. Should a user enter an id in the address bar, the search can be performed and the results be displayed.
public ActionResult Summary(string id)
{
//simple code
if(true)
{
return RedirectToAction("Index", "Home", HttpVerbs.Post);
}
return View();
}
See this previous answer for some context, but I would agree with the second option listed there. In your case, it might not be a third-party server, but rather your Index action that only accepts POST.
Create the form to post to your server. When the form is submitted, show the user a page that has a form in it with all of the data you want to pass on, all in hidden inputs. Just show a message like "Redirecting...". Then, add a javascript event to the page that submits the form to the third-party server.
In other words, Summary would return a View which, through JavaScript, posted to Index.
Why can't you just return a PartialView of the results back to the Index page?