Search code examples
asp.net-mvc-5

Value getting lost in ASP.NET MVC 5


One of the values from my webpage is not making it to the controller.

I have an OrderDetailsModel model class:

public class OrderDetailsModel
{
    public string SerialNumber { get; set; }
    public string OrderNumber { get; set; }
}

In my PartsInformationController, I have a small bit of IBM DB2 SQL to get the values:

public class PartsInformationController : Controller
{
    public ActionResult DescriptionTicket(string sdsrno)
    {
        var daorno = "";
        var sql = $@"
SELECT DAORNO
FROM {ViewBag.Library}.DLRINVAPF
WHERE DASRNO='{sdsrno}'";

        using (var cmd = new OleDbCommand(sql, new OleDbConnection(_conn)))
        {
            cmd.Connection.Open();

            using (var reader = cmd.ExecuteReader())
            {
                if (reader.Read())
                {
                    daorno = $"{reader[0]}".Trim();
                }
            }
        }

        var model = new OrderDetailsModel()
        {
            SerialNumber = sdsrno,
            OrderNumber = daorno
        };

        return View(model);
    }
}

In the DescriptionTicket.cshtml view, I have:

@model Website1.Models.OrderDetailsModel
@{
    ViewBag.Title = "DescriptionTicket";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="grid-split">
    <div>
        <h1 class="center">PARTS SEARCH BY KEYWORD</h1>
        <label>
            Search By Keyword <input type="text" id="keyword" value="" /><button onclick="generateByKeyword()">Search</button>
        </label>
    </div>
    <div id="searchResults">

    </div>
</div>
<script>
    function generateByKeyword() {
        var partByKey = '@Url.Action( "PartByKey", "PartsInformation")';
        var orderNumber = 
        $('#searchResults').load(partByKey,
            {
                Keyword: $('#keyword').val(),
                Serial: @Model.OrderNumber,
                dealerID: @Model.DealerID
            });
    }
</script>

screenshot of webpage

When I click the "Search" button, both keyword and dealerID have their values, but the orderNumber parameter is null:

public ActionResult PartByKey(string keyword, string orderNumber, string dealerID)
{
    var model = new PartSearchModel()
        {
            DealerID = dealerID,
            OrderNumber = orderNumber,
        };
}

What am I missing?

Is there a better way to write this other than with Javascript?

screenshot of code


Solution

  • There was a typo in the DescriptionTicket.cshtml view.

    It should have been as follows:

        function generateByKeyword() {
            var partByKey = '@Url.Action( "PartByKey", "PartsInformation")';
            $('#searchResults').load(partByKey,
                {
                    Keyword: $('#keyword').val(),
                    orderNumber: @Model.OrderNumber,
                    dealerID: @Model.DealerID
                });
        }
    

    because the Controller uses the orderNumber variable.