Home > Mobile >  How to get value using razor pages jquery in asp.net entity framework project?
How to get value using razor pages jquery in asp.net entity framework project?

Time:12-28

I have a cshtml file with a form:

<div >
                            <form id="ticketdata" action="/Ticket/[email protected]" method="post">
                                <input type="hidden" asp-for="@Model.TicketId" />
...
...
...
<button id="btnticket" type="submit"  onclick="CreateTicket(">Create Ticket</button>
                            </form>

In the first field I am selecting a Name from a drop down menu who has a unique userId. Based on the UserId I am fetching other details as well(and that's working). Here is the script for that:


$("#CustomerName").change(function () {
            var CustomerName = $("#CustomerName").val();
            $.ajax({
                    url: "../Ticket/FetchCustomerAddress",
                    data: { userId: CustomerName },
                    type: "Get",
                    success: function (data) {
                        $('#Address').val(data.address);
                        $('#City').val(data.city);
                        $('#ZipCode').val(data.zipCode);
                        $('#CustomerName').val(data.customerId)
                    }

After filling the form, at the time of submission. I want that userId for further operations. But I am not getting those details. I am only getting other details in the form and not the selected userId.

In the Ticket model, I have a parameter userId.

How to get that userId in the controller? using jquery or javascript?

CodePudding user response:

In the model define one property as "ExtraPropertyId"& on View store your userId in that now you can pass value in that property of the controller.

CodePudding user response:

In the first field I am selecting a Name from a drop down menu who has a unique userId.

Asp .Net Core bind data with name attribute,you can bind the userId with <select.</select> by setting its name attribute.

<select name="UserId">
...
</select>

So that when you submit the form,the selected value will be sent to controller as UserId by default.If you want to get the selected UserId in js,you can use $("#UserId").val();.

  • Related