Hiddenfor - Submit loses value the first time

Asked

Viewed 130 times

1

Follows code:

Index:

@{
    ViewBag.Title = "Home Page";
}

<div class="modal fade" id="minhaModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div id="conteudoModal"></div>
        </div>
    </div>
</div>

<div class="row">
    <button class="btn btn-default" id="myclick">click me</button>
</div>


<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>

<script type="text/javascript">

    $("#myclick").click(function () {
        $("#conteudoModal").load("@Url.Action("_minhaPartialView", "Home")", function () {
            $("#minhaModal").modal("show");
        });
    });
</script>

_myPartialView:

@model WebApplication1.Models.Modelos

@using (Ajax.BeginForm("MyAction", "Home", new AjaxOptions { HttpMethod = "POST", OnBegin = "OnBegin_Function", OnSuccess = "OnSuccess_Function" }, new { @class = "form-horizontal", role = "form" }))
{
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Título</h4>
    </div>

    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.MyProperty)

    <div class="modal-footer">
        <button type="submit" class="btn btn-success">Salvar</button>
        <button type="button" class="btn btn-danger" data-dismiss="modal">Cancelar</button>
    </div>
}

<script type="text/javascript">

 function OnBegin_Function() {
     $('#MyProperty').val(123);
    }

    function OnSuccess_Function() {

    }

</script>

Controller:

[HttpGet]
public ActionResult _minhaPartialView()
{
    var model = new Modelos
    {
        MyProperty = 1
    };

    return PartialView(model);
}

public ActionResult MyAction(Modelos model)
{
    // aqui retorna model.MyProperty = 1 na primeira vez.
    // o certo é retornar valor 123, por exemplo
    return Json(true, JsonRequestBehavior.AllowGet);
}

Problem:

When I do "Submit", the value returns 1 the first time. The second time returns 123.

UPDATE:

$(function () {
    $('#MyProperty').val(123);  //AQUI FUNCIONA !!!
});

function OnBegin_Function() {  //NÃO FUNCIONA
    $("#MyProperty").val(123);      
}
  • You are making the request via ajax ?

  • See if he can help you How to make return with Ajax Beginform this answer has two ways to make the request.

  • Yes, in this answer he is showing how to send the form to the server and receive its return.

  • strange his attempt with the .val() not working, here in my code I have this field @Html.HiddenFor(model => model.Radius), and in JS do it $('#Radius').val($(this).val());, it receives the value typed in an input, important to say that this is possible also after the screen is fully loaded, put this in a $(document).ready().

  • @Matheusmiranda wouldn’t it be better if you put the Javascript complete in question ?

1 answer

1


You are having this problem because you are trying to change a form value after firing the "Onbegin".

According to the MSDN:

The Javascript Function is called by ASP.NET MVC after the Httprequest Object is instantiated but before it is Invoked

Therefore, this property is called after the form serialization. Taking this into account, it is not possible to change the receipt of data in your Controller, only in your input. Try to change the values before your serialization form.

  • 1

    I’ve been killing myself here all day, thanks for the helpful reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.