MVC搜索框

时间:2015-03-23 18:36:36

标签: c# asp.net-mvc linq

我有一个MVC视图,应允许用户按ID号或姓氏进行搜索。他们会在id文本框中输入id,或在姓氏文本框中输入姓氏。现在使用我的代码,只有输入id和姓氏,或只输入id号,它才会起作用。

这是我控制器中的代码。

 [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            int id = Convert.ToInt32(collection["ID_NUM"]);
            string last = collection["LAST_NAME"];


            var ViewResults = new CombinedModels();

ViewResults.ADDRESS_MASTER = (from e in db.ADDRESS_MASTER
                                          where e.ID_NUM == id
                                          select e).FirstOrDefault();

            ViewResults.NAME_AND_ADDRESS = (from n in db.NAME_AND_ADDRESS
                where n.ID_NUM == id || n.LAST_NAME == last
                select n).FirstOrDefault();


            return View(ViewResults);
        }

第二个LINQ语句是我试图用于搜索的语句。

以下是我的观点。

@*@model JHelpWebTest2.Models.NAME_AND_ADDRESS*@
@model JHelpWebTest2.Models.CombinedModels



<link href="~/css/StyleSheet.css" rel="stylesheet" />
<link rel="shortcut icon" href="~/images/dragonball3.ico">

@{
    ViewBag.Title = "JHelp";
}


@using (Html.BeginForm("Index", "JHelp"))
{
    <section class="section1">
        <div>
            <label>Union ID</label>
            @Html.TextBox("ID_NUM")           
            <label>Last Name</label>
            @Html.TextBox("LAST_NAME")
            <input type="submit" value="Search"/><br/>
        </div>
    </section>
    <section class="section1">
        <div class="divWidth">
            <label>Password</label>
            @Html.TextBox("Password")
        </div>
        <div class="divWidth">
            <input type="submit" value="Update"/><br/>
            <input type="submit" value="Reset"/>
        </div>
    </section>
    if (Model != null)
    {
        <section class="section1">
            <section class="section2">
                <div>
                    <label class="alignLabel">First Name</label>
                    @Html.TextBoxFor(model => model.NAME_AND_ADDRESS.FIRST_NAME, new { @class="alignTextBox", size="5"})
                    <label class="alignLabel">Last Name</label>
                    @Html.TextBoxFor(model => model.NAME_AND_ADDRESS.LAST_NAME, new { @class="alignTextBox", size="5"})
                </div>
            </section>
            <section class="section2">
                <div class="divWidth">
                    <label class="alignLabel">Prefix</label>
                    @Html.TextBoxFor(model => model.NAME_AND_ADDRESS.PREFIX, new { @class="alignTextBox", size="5"})<br/>
                    <label class="alignLabel">Preferred Name</label>
                    @Html.TextBoxFor(model => model.NAME_AND_ADDRESS.PREFERRED_NAME, new { @class = "alignTextBox", size="5" })
                </div>
            </section>
            <section class="section2">
                <div class="divWidth">
                    <label class="alignLabel">Suffix</label>
                    @Html.TextBoxFor(model => model.NAME_AND_ADDRESS.SUFFIX, new { @class="alignTextBox", size="5"})<br/>
                    <label class="alignLabel">Middle Name</label>
                    @Html.TextBoxFor(model => model.NAME_AND_ADDRESS.MIDDLE_NAME, new { @class="alignTextBox", size="5"})
                </div>
                </section>
        </section>
        <section class="specialsection">
            <section class="section2">
                <div class="divWidth">
                    <label class="alignLabel">Address Line 1</label>
                    @Html.TextBoxFor(model => model.NAME_AND_ADDRESS.ADDR_LINE_1, new { @class="alignTextBox", size="5"})<br/>
                    <label class="alignLabel">Address Line 2</label>
                    @Html.TextBoxFor(model => model.NAME_AND_ADDRESS.ADDR_LINE_2, new { @class="alignTextBox", size="5"})<br/>
                    <label class="alignLabel">Address Line 3</label>
                    @Html.TextBoxFor(model => model.NAME_AND_ADDRESS.ADDR_LINE_3, new { @class="alignTextBox", size="5"})<br/>
                    <label class="alignLabel">City, State Zip Code</label>
                    @Html.TextBoxFor(model => model.NAME_AND_ADDRESS.CITY, new { @class="alignTextBox", size="5"})
                    @Html.TextBoxFor(model => model.NAME_AND_ADDRESS.STATE, new { @class="alignTextBox", size="5"})
                    @Html.TextBoxFor(model => model.NAME_AND_ADDRESS.ZIP)
                </div>
            </section>
            <section class="section2">
                <div class="divWidth">
                    <label class="alignLabel">Phone</label>
                    @Html.TextBoxFor(model => model.NAME_AND_ADDRESS.PHONE_NUM, new { @class="alignTextBox", size="5"})<br/>
                    <label class="alignLabel">Ext</label>
                    @Html.TextBoxFor(model => model.NAME_AND_ADDRESS.PHONE_EXT, new { @class="alignTextBox", size="5"})<br/>
                    <label class="alignLabel">Country</label>
                    @Html.TextBoxFor(model => model.NAME_AND_ADDRESS.COUNTRY, new { @class="alignTextBox", size="5"})<br/>
                    <label class="alignLabel">County</label>
                    @Html.TextBoxFor(model => model.NAME_AND_ADDRESS.COUNTY, new { @class="alignTextBox", size="5"})
                </div>
            </section>
            <section class="section2">
                <div class="divWidth">
                    @*@if (Model.ADDRESS_MASTER.ADDR_CDE == "*EML")
                    {
                        <label class="alignLabel">*EML</label>
                        @Html.TextBoxFor(model => model.ADDRESS_MASTER.ADDR_LINE_1, new { @class="alignTextBox", size="5"})<br/>
                        <label class="alignLabel">eALT</label>
                        @Html.TextBox("eALT")<br />
                        <label class="alignLabel">eLRN</label>
                        @Html.TextBox("eLRN")

                    }
                    else if (Model.ADDRESS_MASTER.ADDR_CDE =="EALT")
                    {
                        <label class="alignLabel">*EML</label>
                        @Html.TextBox("*EML")<br/>
                        <label class="alignLabel">eALT</label>
                        @Html.TextBoxFor(model => model.ADDRESS_MASTER.ADDR_LINE_1, new { @class="alignTextBox", size="5"})<br />
                        <label class="alignLabel">eLRN</label>
                        @Html.TextBox("eLRN")
                    }
                    else if (Model.ADDRESS_MASTER.ADDR_CDE == "ELRN")
                    {
                        <label class="alignLabel">eLRN</label>
                        <label class="alignLabel">*EML</label>
                        @Html.TextBox("*EML")<br/>
                        <label class="alignLabel">eALT</label><br/>
                        @Html.TextBoxFor(model => model.ADDRESS_MASTER.ADDR_LINE_1, new { @class="alignTextBox", size="5"})
                    }*@
                @switch (Model.ADDRESS_MASTER.ADDR_CDE)
                {
                    case "*EML" : 
                        <label>*EML</label>
                        @Html.TextBoxFor(model => model.ADDRESS_MASTER.ADDR_LINE_1)<br/>
                        <label>eALT</label>
                        @Html.TextBox("eALT")<br />
                        <label>eLRN</label>
                        @Html.TextBox("eLRN")
                        break;

                    case "EALT" : 
                        <label>*EML</label>
                        @Html.TextBox("*EML")<br/>
                        <label>eALT</label>
                        @Html.TextBoxFor(model => model.ADDRESS_MASTER.ADDR_LINE_1)<br />
                        <label>eLRN</label>
                        @Html.TextBox("eLRN")
                        break;

                    case "ELRN" : 
                        <label>eLRN</label>
                        <label>*EML</label>
                        @Html.TextBox("*EML")<br/>
                        <label>eALT</label><br/>
                        @Html.TextBoxFor(model => model.ADDRESS_MASTER.ADDR_LINE_1)
                        break;
                }
                @*<label>*EML</label>
                @Html.TextBoxFor(model => model.ADDRESS_MASTER.ADDR_LINE_1)<br/>
                <label>eALT</label>
                @Html.TextBoxFor(model => model.ADDRESS_MASTER.ADDR_LINE_1)<br/>
                <label>eLRN</label>
                @Html.TextBoxFor(model => model.ADDRESS_MASTER.ADDR_LINE_1)*@

        </div>
        </section>
        </section>
        <section class="section1">
            <div>
                <label>Registration Status</label>



            </div>
            <div>

            </div>
        </section>
    }
}

1 个答案:

答案 0 :(得分:1)

如果用户可以选择输入id,姓氏或两者,则需要动态构建查询。有点像:

int? id = null;
Int32.TryParse(collection["ID_NUM"], out id);
string last = collection["LAST_NAME"];

...

var nameAndAddresses = db.NAME_AND_ADDRESS;
if (id.HasValue)
{
    nameAndAddresses = nameAndAddresses.Where(m => m.ID_NUM == id.Value);
}
if (!string.IsNullOrWhiteSpace(last))
{
    nameAndAddress = nameAndAddresses.Where(m => m.LAST_NAME == last);
}

ViewResults.NAME_AND_ADDRESS = nameAndAddresses.FirstOrDefault();

注意:我将您上面的ID代码切换为使用Int32.TryParse,这更安全。正如你现在所拥有的Convert.ToInt32,如果id返回的结果不是整数,那么app会崩溃。

<强>更新

使用扩展程序存在危险之一。我不倾向于直接使用TryParse,因为它需要一些笨拙的代码。它必须传递一个int,而不是一个可空的int。因此,您可以执行以下操作:

int id;
bool validId = Int32.TryParse(collection["ID_NUM"], out id);

...

if (validId)
{
    nameAndAddresses = nameAndAddresses.Where(m => m.ID_NUM == id);
}

就你的其他错误而言,唯一可预见的原因是你的实际模型属性是DbSet<NAME_AND_ADDRESS>类型。你不应该这样做。 DbSet<T>只应在上下文中使用。在您的视图模型中,您应该使用IEnumerable<T>List<T>等内容。