gpt4 book ai didi

asp.net-mvc-4 - 发布后保留表单值(不是模型的一部分)

转载 作者:行者123 更新时间:2023-12-04 15:24:30 25 4
gpt4 key购买 nike

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

我希望所有表单选择在发布后保留其值,以便用户可以查看(并更改)下一个帖子/搜索的选择。表单填充了 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>
}

数据网格绑定(bind)到模型为
@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>

最佳答案

如果您在 mvc 中使用 html,请查看 here 中的解决方案 2 , value="@Request["txtNumber1"]"对我来说工作得很好,

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

希望对某人有所帮助。

关于asp.net-mvc-4 - 发布后保留表单值(不是模型的一部分),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14224850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com