在我的索引视图上,我需要允许用户输入搜索条件。该条件以FormCollection对象的形式发送回索引控制器。然后,我提取搜索条件,从数据库中提取请求的信息,然后将用户送回索引视图。但是,一旦用户使用请求的信息返回索引视图,来自FormCollection对象的数据现在为空白。

我希望能够将用户的搜索条件保留在我使用的三个文本框中,但是我不确定如何使用FormCollection。有谁知道该怎么做或我应该使用另一个?

视图

@using(Html.BeginForm())
{
    <div id="searchBox" class="boxMe">
        <div id="zipBox" style="float: left; padding-left: 20px; padding-right: 10px; padding-top: 20px; padding-bottom: 20px; vertical-align: top;">
            @Html.Raw("Zip Code")
            @Html.TextArea("ZipSearch", new { style = "width: 300px;", placeholder = "Enter up to 35 comma separated zip codes" })
        </div>
        <div id="dateBox" style="float: left; padding-right: 10px; padding-top: 20px;">
            @Html.Raw("Effective on this date")
            @Html.TextBox("DateSearch", null, new { style="width: 80px;"})
        </div>
        <div id="stateBox" style="float: left; padding-right: 20px; padding-top: 20px;">
            @Html.Raw("State")
            @Html.TextBox("StateSearch", null, new { style = "width: 25px;" })
            <button type="submit">Search</button>
        </div>

    </div>
    <div style="clear: both;"></div>
}


控制者

    public ViewResult Index(FormCollection searchDetails = null)
    {
        string zip = searchDetails["ZipSearch"];
        string date = searchDetails["DateSearch"];
        string state = searchDetails["StateSearch"];

        if (String.IsNullOrWhiteSpace(zip) && String.IsNullOrWhiteSpace(date) && String.IsNullOrWhiteSpace(state))
        {
            return View();
        }

        string[] zipArray;
        DateTime effectiveDate;

        //Convert date string to DateTime type
        if (String.IsNullOrWhiteSpace(date))
        {
            effectiveDate = DateTime.MinValue;
        }
        else
        {
            effectiveDate = Convert.ToDateTime(date);
        }

        //Conduct search based on Zip Codes
        if (!String.IsNullOrWhiteSpace(zip))
        {
            //Create array and remove white spaces
            zipArray = zip.Split(',').Distinct().ToArray();
            for (int i = 0; i < zipArray.Length; i++)
            {
                zipArray[i] = zipArray[i].Trim();
            }

            //Add zip codes to list object then send back to view
            List<ZipCodeTerritory> zips = new List<ZipCodeTerritory>();

            if (String.IsNullOrWhiteSpace(state))
            {
                foreach (var items in zipArray)
                {
                    var item = from z in db.ZipCodeTerritory
                               where z.ZipCode.Equals(items) &&
                                     z.EffectiveDate >= effectiveDate
                               select z;
                    zips.AddRange(item);
                }
            }
            else
            {
                foreach (var items in zipArray)
                {
                    var item = from z in db.ZipCodeTerritory
                               where z.ZipCode.Equals(items) &&
                                     z.EffectiveDate >= effectiveDate
                               select z;
                    zips.AddRange(item);
                }
            }

            return View(zips);
        }

        //Zip code was not specified so search by state
        if (!String.IsNullOrWhiteSpace(state))
        {
            var items = from z in db.ZipCodeTerritory
                        where z.StateCode.Equals(state) &&
                              z.EffectiveDate >= effectiveDate
                        select z;

            return View(items);
        }

        //Neither zip code or state specified, simply search by date
        var dateOnly = from z in db.ZipCodeTerritory
                    where z.EffectiveDate >= effectiveDate
                    select z;

        return View(dateOnly);
    }


编辑

按照下面的说明,我已经按照如下方式创建了View模型:

public class ZipCodeIndex
{
    public List<ZipCodeTerritory> zipCodeTerritory { get; set; }
    public string searchZip { get; set; }
    public string searchDate { get; set; }
    public string searchState { get; set; }
}


但是在我看来,我无法访问任何这些属性。视图的标题是这样写的:

@model IEnumerable<Monet.ViewModel.ZipCodeIndex>


但是,所有TextBoxForTextAreaFor助手都说不存在任何指定的属性。

@using(Html.BeginForm("Index", "ZipCodeTerritory", FormMethod.Post))
{
    <div id="searchBox" class="boxMe">
        <div id="zipBox" style="float: left; padding-left: 20px; padding-right: 10px; padding-top: 20px; padding-bottom: 20px; vertical-align: top;">
            @Html.Raw("Zip Code")
            @Html.TextAreaFor(model => model.searchZip, new { style = "width: 300px;", placeholder = "Enter up to 35 comma separated zip codes" })
        </div>
        <div id="dateBox" style="float: left; padding-right: 10px; padding-top: 20px;">
            @Html.Raw("Effective on this date")
            @Html.TextBoxFor(model => model.searchDate, null, new { style="width: 80px;"})
        </div>
        <div id="stateBox" style="float: left; padding-right: 20px; padding-top: 20px;">
            @Html.Raw("State")
            @Html.TextBoxFor(model => model.searchState, null, new { style = "width: 25px;" })
            <button type="submit">Search</button>
        </div>
    </div>
    <div style="clear: both;"></div>
}


最终编辑

错过了页面正在寻找IEnuerable模型对象的信息。将标题更改为此并解决了问题。

@model Monet.ViewModel.ZipCodeIndex

最佳答案

好的,所以您要做的第一件事就是创建一个模型,只是一个看起来像这样的新类文件。

 public class AddressModel
    {
        public int zip{ get; set; }
        public string state{ get; set; }
        public string date{ get; set; }
    }


然后另一个新的类文件称为视图模型,如下所示。这样,您可以将其用于其他事物。喜欢您的搜索地址,然后将结果返回到单独的列表中。

public class AddressViewModel
    {
        public AddressModel SearchAddress { get; set; }
        public List<AddressModel> ResultsAddress{ get; set; }
    }


然后在您的视图中引用这样的视图模型@model MVCTestProject.ViewModels.InsertPage
在第一行。
然后这些将是您的文本框。

@using (Html.BeginForm("Submit", "Insert", FormMethod.Post))
{
@Html.TextBoxFor(model => model.SearchAddress.zip)
@Html.TextBoxFor(model => model.SearchAddress.state)
@Html.TextBoxFor(model => model.SearchAddress.date)
   <input id="button" name="button" type="submit" value="submit" />
}


在您的控制器中,它与此类似。

 public ActionResult Submit(AddressViewModel model)
        {
          model.ResultsAddress = (from z in db.ZipCodeTerritory
                        where z.StateCode.Equals(state) &&
                              z.EffectiveDate >= model.SearchAddress.date
                        select new AddressModel {
                        date = z.effectiveDate }).toList();
            return View("viewName", model);
        }


这将返回原始的搜索条件和结果。可能不是全部都具有100%的功能,但是主要思想在那里,如果您决定走这条路,我可以为您解决问题。

显示结果

@for (int i = 0; i < Model.ResultsAddress.Count; i++)
{
@Html.DisplayFor(model => model.ResultsAddress[i].date)
}


或者仅用于显示它们,可以用于编辑和重新提交数据。

@foreach (var item in Model.ResultsAddress)
{
    <div>@item.date</div>
}

08-26 14:48