Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

.net - send @Model from view to controller with ajax


I have reservation form where values from one select tab depend on values from other select tab.
On change of value in one tab ajax is called and sends model to controller without submiting form.
In controller action i cant get @Model value.
What can i do to fix it?
(code works only need to get value of @Model to action in controller)
Thanks for trying to help!!

Here is my form

        <div class="row">
            <div class="col-md-12 form-group">
                <label asp-for="poslovnice">Odabir poslovnice</label>
                <select asp-for="PoslovnicaID" asp-items="Model.poslovnice" class="form-control ">
                    <option selected disabled>Odabir poslovnice</option>

                </select>
                <span asp-validation-for="PoslovnicaID" class="text-danger"></span>
            </div>
        </div>

        <div class="row">
            <div class="col-md-6 form-group">
                <label asp-for="Ime">Ime</label>
                <input type="text" class="form-control" asp-for="Ime">
                <span asp-validation-for="Ime" class="text-danger"></span>
            </div>
            <div class="col-md-6 form-group">
                <label asp-for="Prezime">Prezime</label>
                <input type="text" class="form-control" asp-for="Prezime">
                <span asp-validation-for="Prezime" class="text-danger"></span>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12 form-group">
                <label asp-for="Email">Email</label>
                <input type="email" class="form-control" asp-for="Email">
                <span asp-validation-for="Email" class="text-danger"></span>
            </div>
        </div>
        <div class="row">
            <div class="col-md-6 form-group">
                <label asp-for="brojOsoba">Broj osoba</label>
                <select asp-for="brojOsobaID" asp-items="Model.brojOsoba" class="form-control">
                    <option selected disabled>Odabir</option>

                </select>
                <span asp-validation-for="brojOsobaID" class="text-danger"></span>
            </div>
            <div class="col-md-6 form-group">
                <label asp-for="BrojTelefona">Telefon</label>
                <input type="text" class="form-control" asp-for="BrojTelefona">
                <span asp-validation-for="BrojTelefona" class="text-danger"></span>
            </div>
        </div>

        <div class="row">
            <div class="col-md-6 form-group">
                <label asp-for="DatumRezervacije">Datum rezervacije</label>
                <input type="date" class="form-control" asp-for="DatumRezervacije" onkeypress="return false;">
                <span asp-validation-for="DatumRezervacije" class="text-danger"></span>
            </div>
        </div>
        <div class="row">
            <div class="col-md-6 form-group">
                <label asp-for="TerminRezervacije">Termin</label>
                <select asp-for="TerminRezervacijeID" asp-items="Model.TerminRezervacije" class="form-control">
                    <option selected disabled>Termin</option>

                </select>
                <span asp-validation-for="TerminRezervacijeID" class="text-danger"></span>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12 form-group">
                <label asp-for="Napomena">Message</label>
                <textarea class="form-control" asp-for="Napomena" cols="30" rows="7"></textarea>
                <span asp-validation-for="Napomena" class="text-danger"></span>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12 form-group">
                <input type="submit" class="btn btn-primary btn-lg btn-block" value="Reserve Now" data-save="modal">
            </div>
        </div>

    </form>

my js code

<script>




$("select, #DatumRezervacije").change(function () {
   
    //window.alert("aa");
     $.get("/Rezervacija/RezervacijaPartial?mod=@Model", function (data) {
        $("#rezervacijaPlaceholder").html(data)
    })

})

and my controller action

 public IActionResult RezervacijaPartial(RezervacijaVM mod)
    {
        RezervacijaVM model = new RezervacijaVM();

        
          //logic

        return PartialView(model);
    }
question from:https://stackoverflow.com/questions/65660710/send-model-from-view-to-controller-with-ajax

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a whole working demo:

Model:

public class RezervacijaVM
{
    public int PoslovnicaID { get; set; }
    public int brojOsobaID { get; set; }
    public int TerminRezervacijeID { get; set; }
    public IEnumerable<SelectListItem> poslovnice { get; set; }
    public IEnumerable<SelectListItem> brojOsoba { get; set; }
    public IEnumerable<SelectListItem> TerminRezervacije { get; set; }
    public string Ime { get; set; }
    public string Prezime { get; set; }
    public string Email { get; set; }
    public string BrojTelefona { get; set; }
    public string Napomena { get; set; }
    public DateTime DatumRezervacije { get; set; }
}

View:

@model RezervacijaVM
<form id="form1">
    <div class="row">
        <div class="col-md-12 form-group">
            <label asp-for="poslovnice">Odabir poslovnice</label>
            <select asp-for="PoslovnicaID" asp-items="Model.poslovnice" class="form-control ">
                <option selected disabled>Odabir poslovnice</option>

            </select>
            <span asp-validation-for="PoslovnicaID" class="text-danger"></span>
        </div>
    </div>

    <div class="row">
        <div class="col-md-6 form-group">
            <label asp-for="Ime">Ime</label>
            <input type="text" class="form-control" asp-for="Ime">
            <span asp-validation-for="Ime" class="text-danger"></span>
        </div>
        <div class="col-md-6 form-group">
            <label asp-for="Prezime">Prezime</label>
            <input type="text" class="form-control" asp-for="Prezime">
            <span asp-validation-for="Prezime" class="text-danger"></span>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12 form-group">
            <label asp-for="Email">Email</label>
            <input type="email" class="form-control" asp-for="Email">
            <span asp-validation-for="Email" class="text-danger"></span>
        </div>
    </div>
    <div class="row">
        <div class="col-md-6 form-group">
            <label asp-for="brojOsoba">Broj osoba</label>
            <select asp-for="brojOsobaID" asp-items="Model.brojOsoba" class="form-control">
                <option selected disabled>Odabir</option>

            </select>
            <span asp-validation-for="brojOsobaID" class="text-danger"></span>
        </div>
        <div class="col-md-6 form-group">
            <label asp-for="BrojTelefona">Telefon</label>
            <input type="text" class="form-control" asp-for="BrojTelefona">
            <span asp-validation-for="BrojTelefona" class="text-danger"></span>
        </div>
    </div>

    <div class="row">
        <div class="col-md-6 form-group">
            <label asp-for="DatumRezervacije">Datum rezervacije</label>
            <input type="date" class="form-control" asp-for="DatumRezervacije" onkeypress="return false;">
            <span asp-validation-for="DatumRezervacije" class="text-danger"></span>
        </div>
    </div>
    <div class="row">
        <div class="col-md-6 form-group">
            <label asp-for="TerminRezervacije">Termin</label>
            <select asp-for="TerminRezervacijeID" asp-items="Model.TerminRezervacije" class="form-control">
                <option selected disabled>Termin</option>

            </select>
            <span asp-validation-for="TerminRezervacijeID" class="text-danger"></span>
        </div>
    </div>

    <div class="row">
        <div class="col-md-12 form-group">
            <label asp-for="Napomena">Message</label>
            <textarea class="form-control" asp-for="Napomena" cols="30" rows="7"></textarea>
            <span asp-validation-for="Napomena" class="text-danger"></span>
        </div>
    </div>

    <div class="row">
        <div class="col-md-12 form-group">
            <input type="submit" class="btn btn-primary btn-lg btn-block" value="Reserve Now" data-save="modal">
        </div>
    </div>

</form>

JS:

@section scripts
{
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}

    <script>
            $("select, #DatumRezervacije").change(function () {
                var data = $("#form1").serialize();
                console.log(data);
                $.get("/Rezervacija/RezervacijaPartial?"+data, function (data) {
            $("#rezervacijaPlaceholder").html(data)
        })

    })
    </script>
}

Result:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...