0

How will I properly display data from database to textbox when the button "search" is clicked? It should display the item name and the price of that product code

enter image description here

Controller

public ActionResult Index(string searchString)
{
    using(MyDatabaseEntities db = new MyDatabaseEntities())
    {
        var products = from s in db.Products
                       select s;
        if (!String.IsNullOrEmpty(searchString))
        {
            products = products.Where(s => s.ProductCode.Equals(searchString));
        }
        return View(products.ToList());
    }
}

Index

<div class="form-group">
    <table style="text-align:left; margin-bottom:10px;">
        <tr>
            <td>Item Code</td>
            <td>@Html.TextBox("SearchString")
        </td>
        <tr style="background-color:lightgray;">
            <td colspan="2"></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" class="btn btn-block btn-info"id="buttonsearch" value="Search" />
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>  
        @foreach (var item in Model) { 
            <tr>
                <td style="text-align:left;">Item Name</td
                <td>@Html.DisplayFor(modelItem=> item.ProductName)</td>
            </tr>
            <tr>
                <td>Unit Price</td>
                <td style="text-align:right;">@Html.DisplayFor(modelItem => item.Price)</td>
            </tr>
        }
        <tr>
            <td style="text-align:left;">Quantity</td>
            <td>
                <input type="text" id="quantity" class="input-sm" />
                <span class="error">Product Name  required</span>
            </td>
        </tr>
        <tr>
            <td style="text-align:left;">Discount</td>
            <td>
                <input type="text" id="discount" class="input-sm" style="text-align:right;" placeholder="0.00" />
                <span class="error">Product Name required</span>
            </td>
        </tr>
    </table>
</div>
Pearl
  • 21
  • 1
  • 7
  • so, where is your problem? – teo van kot May 24 '16 at 18:02
  • when I clicked the search button, all data including those product that has different product code has been displayed in the textbox – Pearl May 24 '16 at 19:02
  • You can use ajax to post your 'searchString' and return a partial view of the products and then update the DOM with the view that was returned (or you could just put the `@Html.TextBox("SearchString")` inside a form with `FormMethod.Get` but that would mean reloading the page each time. –  May 25 '16 at 04:27
  • But you cannot use a `foreach` loop to generate controls for you collection (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) but its not clear what your doing with that since you have not shown any form tags in your view. –  May 25 '16 at 04:29

1 Answers1

0

Instead of .ToList() use .FirstOrDefault(); just in case you want to populate one record in your textbox.

Poonam
  • 153
  • 1
  • 12
  • shows an error message The model item passed into the dictionary is of type 'Final_SPARK_OSIMS.Product', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Final_SPARK_OSIMS.Product]'. – Pearl May 24 '16 at 19:20
  • @Pearl try adding this piece in your View: '@model IEnumerable' Hope this helps. – Rohan Rao Oct 04 '19 at 10:42