views:

274

answers:

3

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: 

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" />
<% } %>
tvanfosson
Ok, that is what I had initially but it keeps posting to the Details POST. could it be due to the structure of my form, you see inside my Details View I am calling in the TaskStatus links by using this <% Html.RenderPartial("TaskStatus"); %>and inside that TaskStatus I have what you provided. Thanks for your prompt reply by that way.
Geovani Martinez
Do you have nested form tags? Soemthing like acceptform is child of detailform?
Malcolm Frexner
I think it's best if you had a look at the source. Not only is it helping me learn mvc but hopefully it helps out others when i release the first version.http://bettertasklist.unfuddle.com/svn/bettertasklist_bettertasklist/username: stackoverflowpassword: helpout
Geovani Martinez
Your TaskDetails wraps a form around everything...and you can't nest forms. I don't see that you actually need a form on the TaskDetails, though, as you are merely displaying the details. I suggest removing the form from the details page. Also, the reason I think that you are getting the error is that you are using an AJAX ActionLink which uses GET, not POST. Change the method on the AJAX options to use POST or build the AJAX form (similar to what I've shown) instead -- or change your method to accept a GET request instead of a POST (that might be easier).
tvanfosson
thank you!!!! the "and you can't nest forms" was the big eye opener and got everything mentioned above working like it should(after un-nesting them). truly appreciate everyone's help.
Geovani Martinez
A: 

A link (<a>) cannot post a form, so you have three choices:

  1. Use a button instead;
  2. Use an <input type="image"> instead, which you can finesse to look similar to a link;
  3. 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).

Aaronaught
Thank you very much. I have a clear picture of the solution and it makes good sence. I have tried both ways and each time I get this error "The resource cannot be found. Requested URL: /BetterTaskList/Task/AcceptTask/6" so it must be something else that I am doing wrong. thanks again.
Geovani Martinez
A: 

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.

Malcolm Frexner