搜索视图:MVC

时间:2016-06-12 18:51:45

标签: asp.net-mvc web view asp.net-mvc-controller

我需要制作一个“搜索和过滤视图”

我的问题:

我想创建一个搜索视图,用户可以选择要搜索的属性,然后在文本框中输入内容并提交。视图比控制器找到它需要找到的内容并显示它

我的观点是在控制器中的HttpPost函数完成时编写的部分。但它很早就进入了。我在这里的控制器代码部分显示结果时遇到问题:

public ActionResult Filter()
{
    return View();
}


[HttpPost]
public ActionResult Filter(string option, string search, FormCollection fc)
{
    var bll = new STUDIJSKIPROGRAMBLLProvider();
    StudijskiProgramViewModel model = new StudijskiProgramViewModel();

    if (option == "skracenicaSmjer")
    { 
        // For now I just putted some random number to get some data displayed

        var program = bll.Fetch(2004);
        model.StudijskiProgram = program;
        return View(model);
    }
    else if (option == "skracenicaProfil")
    {
        return View();
    }
    else
    {
        return View();
    }
}

并查看代码:

@model SolutionC.Models.StudijskiProgramViewModel

@{
    ViewBag.Title = "Pretraga studijskih programa";
}

@using (Html.BeginForm("Filter", "StudijskiProgram", FormMethod.Get))
{
    <h2>Pretraga studijskih programa</h2>

    <b> Kriteriji pretrage: </b>

    <input type="radio" name="prvi" value="prvi1"/> <text>Skraćenica profil</text> 

    @Html.RadioButton("option", "skracenicaSmjer") <text>Skraćenica smjer</text>

    @Html.RadioButton("option", "skracenicaProfil") <text> Skraćenica profil </text>

    @Html.RadioButton("option", "ECTSOpterecenje")<text>ECTS</text> 

    @Html.TextBox("search") 

    <input type = "submit" name = "submit" value = "Traži" />  

}
@using (Html.BeginForm("Filter", "StudijskiProgram", FormMethod.Post)) 
//Don't want my view to display this until i click Submit and my controller sets up data for display
{
<table class="table table-striped">
        <thead class="alert alert-info">
            <tr>
                <th>
                   Naziv smjera:
                </th>
                <th>
                    Naziv profila:
                </th>
                <th>
                    Šifra:
                </th>



            </tr>
        </thead>
        <tr>
            <td> @Model.StudijskiProgram.smjer </td>  //Compilation fails here
            <td> @Model.StudijskiProgram.profil</td>
            <td> @Model.StudijskiProgram.IDStudProg </td>
        </tr>
</table>
}

1 个答案:

答案 0 :(得分:0)

你错了。

@using (Html.BeginForm("Filter", "StudijskiProgram", FormMethod.Post))

仅表示“使我具有以下属性的表单标记包装”仅在发布按钮时显示此内容。

您应该使用条件来包装它,只有在设置了model属性时才会满足。

if (Model.SomeProperty)
{
    <table class="table table-striped">
    ...
}

bool SomePropery应在您的模型上声明,并且当您希望显示帖子部分时,设置为 true

注意:当您希望视图了解您的模型时,您应该返回View(model)