Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my code
for view


HTML
@model Logs.Models.Fuel

@{
    ViewBag.Title = "Fuel LOG";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2 align="center">
    <u>
        FUEL AND ASH LOG
    </u>
</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<script type="text/javascript">
    function fn_Search() {
        debugger;      
        var url= '@Url.Action( "Search", "Fuel")';
        $.get(url, {
            EntryDate: $("#EntryDate").val(), Shift: $("#Shift").val()
        }, function (data) {
            $("#Search").html(data);
        });
      

    }
    $(function () { $("#EntryDate").datepicker({ dateFormat: 'dd/mm/yy', changeYear: true }); });
</script>


<style type="text/css">
    td {
        border-color: black;
    }

    th {
        border-color: black;
    }

    .Width1 {
        width: 50px;
    }
</style>

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <div style="width:100%;overflow-x:scroll;">

        <table>
            <tr>
                <td>
                    <button type="submit" id="btnFirst" name="Command" value="First">First</button>
                </td>
                <td>
                    <button type="submit" id="btnPrevious" name="Command" value="Previous">Previous</button>
                </td>
                <td>
                    <button type="submit" id="btnNext" name="Command" value="Next">Next</button>
                </td>
                <td>
                    <button type="submit" id="btnLast" name="Command" value="Last">Last</button>
                </td>
            </tr>
        </table>
        <br /><br />
        <table border="1" style="width:100%;">
            <tr>
                <td>
                    Name of Incharge Engineer:
                </td>
                <td>
                    @Html.DisplayFor(model => model.UserName)
                </td>
                <td rowspan="2">
                    Date
                    @Html.EditorFor(model => model.EntryDate, null, "EntryDate",null)
                    @Html.ValidationMessageFor(model => model.EntryDate)

                </td>
                <td rowspan="2">
                    Shift
                    @Html.DropDownListFor(model => model.Shift, (IEnumerable<SelectListItem>)TempData["ShiftOption"], "Select One")

                </td>
                <td rowspan="2">
                    <button type="button" id="btnSearch" name="Command" value="Search" onclick="return fn_Search();">Search</button>
                </td>
            </tr>
            <tr>
                <td>
                    Name of Incharge Engineer:
                </td>
                <td>
                    @Html.DisplayFor(model => model.ShiftEngineer)
                </td>
            </tr>
        </table>
        <br />


and in controller

C#
public class FuelController : Controller
   {
       // GET: Fuel
       public ActionResult Index(string shift)
       {
           Fuel f = new Fuel();
           TempData["ShiftOption"] = f.ShiftList();
           f.EntryDate = cl.GetNextDay(4, "", "Fuel",Convert.ToInt16(shift));
           return View(f.GetFuel(f,f.EntryDate,shift));
       }

       [HttpPost]
       public ActionResult Index(Fuel fuel, string command)
       {
           if (command == "Submit")
           {
               if (ModelState.IsValid)
               {
                   DataTable dt = new DataTable();
                   dt.Columns.Add("id");
                   dt.Columns.Add("Name");
                   dt.Columns.Add("Value");



                   dt.Rows.Add("0", "Rake1", fuel.Rake1);
                   dt.Rows.Add("0", "Rake2", fuel.Rake2);
                   // so on



                   // fuel.SaveUpdate(fuel, dt, Convert.ToInt32(Session["UserId"].ToString()));
                   fuel.SaveUpdate(fuel, dt, 1);
               }
               TempData["ShiftOption"] = fuel.ShiftList();
               return View();
           }
           else if (command == "Search")
           {
               string EntryDate = fuel.EntryDate;
               string Shift = fuel.Shift;
               Fuel f = new Fuel();
               TempData["ShiftOption"] = f.ShiftList();
               fuel = f.GetFuel(f, EntryDate, Shift);
               return View(f.GetFuel(f, EntryDate, Shift));
           }
           else
           {
               return View();
           }



       }

       ConnectionStringClass cl = new ConnectionStringClass();



       public ActionResult  Search(string  EntryDate,string Shift)
       {

           Fuel f = new Fuel();
           TempData["ShiftOption"] = f.ShiftList();
           return View("Index",f.GetFuel(f, EntryDate, Shift));

       }


   }


Here the Search method is fired but view is not loading the result data


What I have tried:

I have tried to do in Http post request in
Index(Fuel fuel, string command) but it is not loading the result data to view.
Posted
Updated 7-Apr-16 22:29pm

1 solution

$("#Search").html(data);


That puts the results in an element with an id of Search, but there is no such element on your page. You need something on the page like

HTML
<div id="Search"></div>


Index.cshtml

Razor
@model SearchModel
<h1>Search</h1>

@using (Html.BeginForm())
{ 
    <p>
        Search: @Html.TextBoxFor(m => m.SearchText)<br/>
        <input type="submit" value="Search" id="submit"/>
    </p>

    <div id="Search">
        @Html.Partial("_SearchResults", Model)
    </div>
}
<script type="text/javascript">
    $(document).ready(function () {
        $("#submit").click(function (e) {
            e.preventDefault();

            var url = '@Url.Action( "Search", "Home")';
            $.get(url, {
                searchText: $("#SearchText").val()
            }, function (data) {
                $("#Search").html(data);
            });
        });
    });
</script>



_SearchResults.cshtml

Razor
@model TestMVC.Models.SearchModel

@if (Model.Results == null || Model.Results.Count == 0)
{
    <p>No search results</p>
    return;
}

<table>
    @foreach(string result in Model.Results)
    { 
    <tr>
        <td>
            @result
        </td>
    </tr>
    }
</table>



SearchModel

C#
public class SearchModel
{
    public string SearchText { get; set; }
    public List<string> Results { get; set; }

    public void PopulateSearchResults()
    {
        this.Results = new List<string>();

        this.Results.Add("Result 1 for " + this.SearchText);
        this.Results.Add("Result 2 for " + this.SearchText);
        this.Results.Add("Result 3 for " + this.SearchText);
    }
}


Controller

C#
[HttpGet]
public ActionResult Index()
{
    SearchModel model = new SearchModel();

    return View(model);
}

[HttpPost]
public ActionResult Index(SearchModel model)
{
    model.PopulateSearchResults();

    return View(model);
}

public ActionResult Search(string searchText)
{
    SearchModel model = new SearchModel();

    model.SearchText = searchText;
    model.PopulateSearchResults();

    return PartialView("_SearchResults", model);
}
 
Share this answer
 
v2
Comments
manish.communityhub 8-Apr-16 5:09am    
Yes I have got it. But the whole page loaded twice
F-ES Sitecore 8-Apr-16 5:58am    
Probably because you are returning the entire view (ergo the whole page) so you are getting a page within a page.

The bit you want to be dynamic needs to go inside a partial view and your ajax call replaced just that element with a new partial view rather than the whole view, I'll update my answer with a simplified demo.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900