Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 Post后保留表单值(不是模型的一部分)_Asp.net Mvc 4_Entity Framework 5 - Fatal编程技术网

Asp.net mvc 4 Post后保留表单值(不是模型的一部分)

Asp.net mvc 4 Post后保留表单值(不是模型的一部分),asp.net-mvc-4,entity-framework-5,Asp.net Mvc 4,Entity Framework 5,我有一个MVC4页面,它有一个表单,其中包含一组复选框、单选按钮和文本框,用作搜索字段。post后,将解析选择,并使用新结果更新较低的结果网格。现在,所有表单值在返回时都会被删除,新结果会显示在网格中-只有网格是模型的一部分 我希望所有表单选择在发布后保留其值,以便用户可以查看(并更改)下一次发布/搜索的选择。该表单使用viewbags进行了弹出式上载 @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "sea

我有一个MVC4页面,它有一个表单,其中包含一组复选框、单选按钮和文本框,用作搜索字段。post后,将解析选择,并使用新结果更新较低的结果网格。现在,所有表单值在返回时都会被删除,新结果会显示在网格中-只有网格是模型的一部分

我希望所有表单选择在发布后保留其值,以便用户可以查看(并更改)下一次发布/搜索的选择。该表单使用viewbags进行了弹出式上载

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "searchform" }))
{
    @Html.ValidationSummary("Please correct the following errors")

<div style="float:left;">

    <div style="float:left;">
    <label>Name:</label>
    @Html.TextBox("name")
    </div>

    <div style="float:left; margin-left:15px">
    <label>Company:</label>
    @Html.TextBox("company")
    </div>

    <div style="float:left; margin-left:65px">
    <label>Date Range:</label>
    @Html.TextBox("dateStart", "", new { @class = "datefield", type = "date" })
    &nbsp;to&nbsp;
    @Html.TextBox("dateEnd", "", new { @class = "datefield", type = "date" })
    </div>

</div>

<div style="clear: both;">
    Match Any Categories? <input type="radio" name="categoryMatchAll" value="false" checked="checked" />&nbsp;&nbsp;&nbsp;
    Match All Categories?  <input type="radio" name="categoryMatchAll" value="true" /> 
</div>

<div style="float:left;">

    <div id="searchform-categories" style="float:left;">
        <div class="scroll_checkboxes">
            <label>Categories</label>
            <ul>
            @foreach (var x in ViewBag.Categories)
            {
                    <li>
                        <input type="checkbox" name="categories" value="@x.Id"/>

                        @x.Name

                    </li>
            }
            </ul>
        </div>
    </div>

    <div id="searchform-diversity" style="float:left; margin-left:30px">    
        <div class="search-selection" style="float:left;">
            <label>Minority Owned</label>
            <ul>
                @foreach (var x in ViewBag.Minorities)
                {
                    <li>
                        @Html.RadioButton("minorities", (String)x.Id.ToString())
                        @x.Name
                    </li>
                }
            </ul>
        </div>
        <div class="search-selection" style="float:left;">
            <label>Diversity Class</label>
            <ul>
            @foreach (var x in ViewBag.Classifications)
            {
                <li>
                    @Html.RadioButton("classifications", (String)x.Id.ToString())
                    @x.Name
                </li>
            }
        </ul>
        </div>    
    </div>  

</div>    

<div style="clear: both;">
    <input type="submit" value="Search Profiles" />
    <input type="submit" value="Reset" />
    </div>       
}
@使用(Html.BeginForm(“Index”,“Home”,FormMethod.Post,new{id=“searchform”}))
{
@ValidationSummary(“请更正以下错误”)
姓名:
@Html.TextBox(“名称”)
公司:
@Html.TextBox(“公司”)
日期范围:
@TextBox(“dateStart”,new{@class=“datefield”,type=“date”})
到
@TextBox(“dateEnd”,new{@class=“datefield”,type=“date”})
匹配任何类别?
匹配所有类别?
类别
    @foreach(ViewBag.Categories中的变量x) {
  • @x、 名字
  • }
少数民族所有
    @foreach(ViewBag.com中的变量x) {
  • @RadioButton(“少数民族”,(字符串)x.Id.ToString() @x、 名字
  • }
多样性类
    @foreach(ViewBag.Classification中的变量x) {
  • @RadioButton(“分类”,(字符串)x.Id.ToString() @x、 名字
  • }
}
数据网格绑定到模型,如下所示

@model IEnumerable<VendorProfileIntranet.Models.VendorProfile>

<table id="VendorTable" width="100%" class="gradeA">
<thead>
    <tr>
    <th>
        @Html.DisplayNameFor(model => model.Name)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.CompanyName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.City)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.State)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.DateCreated)
    </th>
    <th>Actions</th>
    </tr>
</thead>
<tbody>

@foreach (var item in Model)
{
<tr>
    <td class="list-field">
        @Html.DisplayFor(modelItem => item.Name)
    </td>
    <td class="list-field">
        @Html.DisplayFor(modelItem => item.CompanyName)
    </td>
    <td class="list-field">
        @Html.DisplayFor(modelItem => item.City)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.State)
    </td>
    <td class="list-field">
        @Html.DisplayFor(modelItem => item.DateCreated)
    </td>
    <td class="list-field">
        @Html.ActionLink("Edit", "Edit", new { id = item.ProfileID }) |
        @Html.ActionLink("View", "View", new { id = item.ProfileID }) |
        @Html.ActionLink("Delete", "Delete", new { id = item.ProfileID }, new { onclick = " return DeleteConfirm()" })
    </td>
</tr>

}

</tbody>
<tfoot>
    <tr>
        <td>&nbsp;</td>
    </tr>
</tfoot>
@model IEnumerable
@DisplayNameFor(model=>model.Name)
@DisplayNameFor(model=>model.CompanyName)
@DisplayNameFor(model=>model.City)
@DisplayNameFor(model=>model.State)
@DisplayNameFor(model=>model.DateCreated)
行动
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.Name)
@DisplayFor(modelItem=>item.CompanyName)
@DisplayFor(modeleItem=>item.City)
@DisplayFor(modelItem=>item.State)
@DisplayFor(modelItem=>item.DateCreated)
@ActionLink(“编辑”,“编辑”,新的{id=item.ProfileID})|
@ActionLink(“视图”,“视图”,新的{id=item.ProfileID})|
@ActionLink(“Delete”,“Delete”,new{id=item.ProfileID},new{onclick=“return DeleteConfirm()”})
}

我通常会将发布的模型传递回视图中。这样就不会清除这些值

您的代码如下所示:

<div style="float:left;">

    <div style="float:left;">
    <label>Name:</label>
    @Html.TextBox("name", Model.Name)
    </div>

<div style="float:left; margin-left:15px">
<label>Company:</label>
@Html.TextBox("company", Model.Company)
</div>

<div style="float:left; margin-left:65px">
<label>Date Range:</label>
@Html.TextBox("dateStart", Model.DateStart, new { @class = "datefield", type = "date" })
&nbsp;to&nbsp;
@Html.TextBox("dateEnd", Model.DateEnd, new { @class = "datefield", type = "date" })
</div>

下面是一篇关于在MVC中为视图创建模型的文章。

所以这里是我通常解决这个问题的方法。我的笔记纯粹是我对MVC项目中的类命名的意见(religous?),以明确它们的用途

两个接口使其保持可扩展性:

// be specific about what type of results, both in the name of the 
// interface and the property needed, you don't want to have overlapping
// properies on your classes, I like suffixing interfaces that are specific
// to a View or Partial View with View
public interface IPersonSearchResultsView
{
   IEnumerable<EFPerson> PersonSearchResults { get; }
}

public interface IPersonSearchCriteriaView
{
  PersonSearchCriteriaModel PersonSearchModel { get; }
}

如果您在mvc中使用html,请从中检查解决方案2,
value=“@Request[“txtNumber1”]”
对我来说效果很好

<input type="text" id="txtNumber1" name="txtNumber1" value="@Request["txtNumber1"]"/>


希望对某些人有帮助。

我的理解是,您只能有一个模型,而且数据网格已经采用了该模型,因此我不确定如何将您的建议合并到一起。(见上文更新)@Vic是的,这是真的,但模型只是一个类。类中可以有任何内容。@在获取时,结果将为空—不显示任何行。搜索完成后,将显示结果和搜索条件。Thx Chuck。我使用的是EF5,因此数据模型类是从数据库自动生成的。我不知道如何扩展它以在我的表单中包含搜索字段。你能提供你建议的更多细节吗?我还看到一些人建议使用TempData,但仍然不确定如何在视图中实现它。@Vic为视图创建一个新模型。这与EF5数据模型是分开的。通常这些模型存储在MVC项目根目录下的Models文件夹中。为什么要否决投票?请解释,以便将来添加更好的答案。
// I like suffixing classes that I only use for MVC with Model
public PersonSearchCriteriaModel
{
  public string Name {get; set;}  
  public string Company {get; set;}
  public string DateStart {get; set;}
  public string DateEnd {get; set;}
}

// I like suffixing classes that I used passed to a View/Partial View
// with ViewModel    
public class PersonSearchViewModel : IPersonSearchResultsView, 
                                     IPersonSearchCriteriaView
{
  public IEnumerable<EFPerson> PersonSearchResults { get; set; }
  public PersonSearchCriteriaModel PersonSearchModel { get; set; }
}
public PersonController : Controller
{
  public ActionResult Search()
  {
    var model = new PersonSearchViewModel();
    // make sure we don't get a null reference exceptions
    model.PersonSearchModel = new PersonSearchCriteriaModel ();
    model.PersonSearchResults = new List<EFPerson>();
    return this.View(model);
  }

  [HttpPost]
  public ActionResult Search(PersonSearchViewModel model)
  {
    model.PersonSearchResults = this.GetPersonResults(model.PersonSearchModel);

    return this.View(model)
  }

  // You could use this for Ajax
  public ActionResult Results(PersonSearchViewModel model)
  {
    model.PersonSearchResults = this.GetPersonResults(model.PersonSearchModel);

    return this.Partial("Partial-SearchResults", model)
  }

  private GetPersonResults(PersonSearchCriteriaModel criteria)
  {
    return DbContext.GetPersonResults(criteria)
  }
}
@model IPersonSearchCriteriaView

// the new part is for htmlAttributes, used by Ajax later
@using (Html.BeginForm(..., new { id="searchCriteria" }))
{
  // Here is were the magic is, if you use the @Html.*For(m=>)
  // Methods, they will create names that match the model
  // and you can back back to the same model on Get/Post

  <label>Name:</label>
  @Html.TextBoxFor(m => Model.PersonSearchModel.Name)

  // or let mvc create a working label automagically

  @Html.EditorFor(m => Model.PersonSearchModel.Name)

  // or let mvc create the entire form..

  @Html.EditorFor(m => Model.PersonSearchModel)
}
@model IPersonSearchResultsView

@foreach (var person in Model.PersonSearchResults )
{
  <tr>
    <td class="list-field">
      @Html.DisplayFor(modelItem => person.Name)
   </td>

   // etc
  </tr>
 }
@model PersonSearchViewModel 

@Html.Partial("Partial-SearchCriteria", Model)

// easily change the order of these

<div id="searchResults">
@Html.Partial("Partial-SearchResults", Model);
</div>
$.Ajax({
  url: '/Person/Results',
  data: $('#searchCriteria').serialize(),
  success: function(jsonResult)
  {
    $('#searchResults').innerHtml(jsonResult);
  });
<input type="text" id="txtNumber1" name="txtNumber1" value="@Request["txtNumber1"]"/>