0

i am new to Ap.net core MVC I am in middle of a project.. i am trying to create a url using jquery and pass that value to controller on form submit.. i can see the value being displayed after i execute a function on button click. I am using loading a partial view on dropdown using jquery im able to populate it and also assign the value to the textbox. But when i try to access the same in controller im not able to find it below is my complete code. I am able to access all the other values but the textbox value which setting through jquery

Below is my View

<form method="post" asp-action="Upsert" enctype="multipart/form-data">

<div class="row px-2 mx-2 backgroundWhite border">
    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    @if (Model.NavId != 0)
    {
        <input type="hidden" asp-for="NavId" />
        title = "Edit Navigation";
    }

    <div class="col-12">
        <h2 class="text-primary">@title</h2>
        <hr class="my-4">
    </div>
    <div class="col-8">
        <div class="form-group row">
            <div class="col-5">
                <label asp-for="Title"></label>
            </div>
            <div class="col-7">
                <input asp-for="Title" class="form-control" />
                <span asp-validation-for="Title" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row">
            <div class="col-5">
                <label asp-for="Position"></label>
            </div>
            <div class="col-7">
                <input asp-for="Position" class="form-control" />
                <span asp-validation-for="Position" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row">
            <div class="col-5">
                <label asp-for="NavigateUrl">NavigationUrl</label>
            </div>
            <div class="col-7">
                <input asp-for="NavigateUrl" disabled id="NavigateUrl"  name="NavigateUrl" class="form-control"   style="color:#000 !important"/>
                <span asp-validation-for="NavigateUrl" class="text-danger"></span>

            </div>
        </div>
        <div class="form-group row">
            <div class="col-5">
                <label>Select Page</label>
            </div>
            <div class="col-7">
                <select id="SelectPage" class="form-control">
                    <option value="-">Select Page</option>
                    <option value="Products">Products</option>
                    <option value="ProductDetails">Product Details</option>
                </select>

            </div>
        </div>

    </div>

    <div class="col-12 selectDiv mt-2 mb-2">

    </div>
    <div class="form-group row col-8">
        <div class="col-12 offset-4">
            @if (Model.NavId != 0)
            {
                //Edit button and back to list
                <partial name="_EditAndBackToListButton" model="Model.NavId" />
            }
            else
            {
                //Create button and back to list
                <partial name="_CreateAndBackToListButton" />
            }
        </div>
    </div>

</div>

@section Scripts{

    <script>
        $(document).ready(function () {
            $('#SelectPage').change(function () {
                var selectedvalue = $(this).children("option:selected").val();
                    $(".selectDiv").load("/Admin/Nav/GetCategoryOrProducts",
                        { option: selectedvalue });
            });
        });

    </script>
    <script>
        var assignvalue = function (primaryId) {

            event.preventDefault();

            var selectedvalue = $('#SelectPage').children("option:selected").val();
            var navtext = $('#NavigateUrl');
            var navurl = "/" + selectedvalue + "/" + primaryId;
            navtext.val(navurl);

        };
    </script>
    @{ await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

My Controller method

    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Upsert(Nav nav)
    {
         if (ModelState.IsValid)
        {
            if (nav.NavId == 0)
            {
                string navurl = HttpContext.Request.Form["NavigateUrl"].ToString(); // I am not able to get the value here it shows null
                nav.NavigateUrl = navurl;
                _unitOfWork.Nav.Add(nav);
            }
            else
            {
                _unitOfWork.Nav.Update(nav);
            }
            return RedirectToAction(nameof(Index));

        }
        return View();
    }
2
  • Hi @Towseed,I could not reproduce your issue.1.Could you share us your _CreateAndBackToListButton.cshtml and how do you call assignvalue function?2.Also,share the action about how to display your main view here. Commented Apr 13, 2020 at 3:25
  • _CreateAndBackToListButton.cshtml has only two buttons create and backtolist, and assignvalue is called on button click. i can see the value is being displayed when i click button but only problem is im not able to access the value in controller Commented Apr 13, 2020 at 19:52

1 Answer 1

1

I am able to access all the other values but the textbox value which setting through jquery

I found that you use disabled in your NavigateUrl.Browsers do not support disabled property to submit with forms by default.

To fix such issue,you could change disabled to readonly:

<input asp-for="NavigateUrl" readonly id="NavigateUrl" name="NavigateUrl" class="form-control" style="color:#000 !important" />
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.