0

I want is to render the employee details of view page ViewByDay.cshtml in SelectDaycshtml page. Concept is to select days in a week from the dropdownlist and to retrieve information related to that day in ViewByDay. In order to do so I used @Html.RenderAction("ViewByDay") in SelectDay. But I get this error

Procedure or function 'ViewByDay' expects parameter '@Days', which was not supplied.

Here are the code snippets of my view classes and the controllers:

SelectDay.cshtml

@model CrudMvc.Models.EmpInfoModels
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h4>Select Employee By Days</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Days, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.Days, (List < SelectListItem >)ViewBag.DayItems, new { @class = "form-control" })

                @Html.ValidationMessageFor(model => model.Days, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Select" class="btn btn-default" />
        </div>
    </div>
}

<div>
    @{ Html.RenderAction("ViewByDay"); }
    // @{ Html.RenderPartial("_ViewByDay");}
    // @Html.Partial("_ViewByDay")
    // @Html.ActionLink("Get All Employee Details", "GetAllEmpDetails","Employee") *@
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

ViewByDay.cshtml

@model IList<CrudMvc.Models.EmpInfoModels>
<div>
    <h4>ListEmpByDays</h4>
    <hr />
    <table class="table">
        <tr>
            <th>Employee Name</th>
            <th>Day</th>
            <th>Destination</th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>@Html.DisplayFor(modelItem => item.EmpName)</td>
                <td>@Html.DisplayFor(modelItem => item.Days)</td>
                <td>@Html.DisplayFor(modelItem => item.Destination)</td>
            </tr>
        }
    </table>
</div>
<p>@Html.ActionLink("Back to List", "GetAllEmpDetails")</p>

Controllers SelectDay and ViewByDay

public ActionResult SelectDay()
{
    var days=  new List<SelectListItem>();
    days.Add(new SelectListItem { Text = "Monday", Value = "Monday" });
    days.Add(new SelectListItem { Text = "Tuesday", Value = "Tuesday" });
    days.Add(new SelectListItem { Text = "Wednesday", Value = "Wednesday" });
    days.Add(new SelectListItem { Text = "Thursday", Value = "Thursday" });
    days.Add(new SelectListItem { Text = "Friday", Value = "Friday" });
    ViewBag.DayItems = days;
    return View();
}

[HttpPost]
public ActionResult SelectDay(string Days)
{           
    return RedirectToAction("ViewByDay", new {Days = Days});
}

public ActionResult ViewByDay(string days)
{
    EmpRepository EmpRepo = new EmpRepository();
    ModelState.Clear();
    var emps = EmpRepo.ViewByDay(days);
    return View(emps);
}
3
  • Remove @{ Html.RenderAction("ViewByDay"); } from SelectDay.cshtml - you have a form and you posting the value of Days and then redirecting to the ViewByDaymethod/view. Side note: ModelState.Clear(); in the ViewByDay() method is pointless (there s nothing in ModelState to clear Commented Aug 15, 2016 at 5:47
  • 1
    Your code is not really making sense. If you want to display the table in the first view, then you want a form with FormMethod.Get and the SelectDay() method needs to have a parameter for Days (and you need to return the model). But you would get far better performance by using ajax to update the DOM based on the selected option Commented Aug 15, 2016 at 6:10
  • Dear friend. I suggest you read about partial views. Then use the same partial view in both ViewByDay and SelectDay. Otherwise I think Stefan Kert answer is correct. Commented Aug 15, 2016 at 15:12

1 Answer 1

2

You need to provide the displayDays as parameter

@Html.Action("ViewByDays", "Controllername", new {days="5"})

Probably the param days should be a integer.

You've already called it correctly in your method SelectDay

[HttpPost]
public ActionResult SelectDay(string Days)
{           
    return RedirectToAction("ViewByDay", new {Days = Days});
}
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.