0

i'm having a hard time getting this to work, all I need to do is update a partial view with some data when a button is clicked. I can't seem to get it to work:

I have a view like this:

@model MyApp.Models.MyModel
<div class="container">
    <div class="myButton">Click Me to update data</div>
    @Html.Partial("_MyPartial")
</div>
<script type="text/javascript">
    $(document).ready(function() {
        $(".myButton").click(function() {
            $.post("/Home/_MyPartial", { "param1" : "@DateTime.Now" }, function(result) {
                $(".myDivToUpdate").html(result);
            });
        });
    });
</script>

Ok, then in my home controller i have an ActionResult method like this:

[HttpGet]
public ActionResult _MyPartial(DateTime param1)
{
    MyModel model = new MyModel();
    /* Update Model */
    return PartialView(model);
}

And in my partial view i have something i need to update dynamically:

<div class="myDivToUpdate">
    @Model.SomeInfo
</div>

Essentially that's the same way i have my stuff laid out. The problem is it never hits the controller when i hit the button in the first place. There may be other issues as I'm no jquery/javascript buff.

I'd appreciate any help on helping me get this to work.

1 Answer 1

2

It's not hitting the controller action because you have marked the controller action with the attribute [HttpGet] which means it will only match on GET requests. Your javascript is using a $.post

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.