locked
Simple question about ActionResult RRS feed

  • Question

  • User562699594 posted

    I have a view with a form that has both the public ActionResult Associate(), and the [HttpPost] public ActionResult Associate().

    Right now the HttpPost is Binding to a model class and I don't want to do that.  I just want to pass some hidden fields.

    How would I do that withing the Bind part of the ActionResult?

    Thanks!

    Thursday, December 7, 2017 10:11 PM

All replies

  • User1400794712 posted

    Hi id3ma,

    If you just want to pass some hidden fields to the HttpPost method, you can use JQuery Ajax. For example:

    @model Test.Models.Category
    
    @{
        ViewBag.Title = "Associate";
    }
    
    <h2>Associate</h2>
    
    @using (Html.BeginForm()) 
    {
        @Html.AntiForgeryToken()
        @*Hidden Fields*@
        @Html.HiddenFor(model=>model.CategoryID)
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.CategoryName, htmlAttributes: new { @class = "control-label col-md-2" })
            @Html.EditorFor(model => model.CategoryName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.CategoryName, "", new { @class = "text-danger" })
        </div>
    
        <input type="submit" value="Edit" class="btn btn-default" id="Edit"/>
    }
    
    @section scripts{
        <script>
            $("#Edit").click(function (e) {
                e.preventDefault();
                var token = $('input[name="__RequestVerificationToken"]', $("form")).val();
                $.ajax({
                    url: '@Url.Action("Associate","Demo")',
                    data: {
                        __RequestVerificationToken: token,
                        ID: $("#CategoryID").val()
                    },
                    method: 'POST',
                    success: function () {
                        alert("s");
                    }
                })
            })
        </script>
        }

    Then in HttpPost method:

    [HttpPost]
    public ActionResult Associate(int? ID)
    {
        return View();
    }

    If you have any confusion, please feel free to post back.

    Best Regards,

    Daisy

    Friday, December 8, 2017 2:02 AM