我有以下部分视图,我试图调用视图(“消息/创建”)内的控制器(“消息”)的操作(“搜索”)。
我似乎根本无法触发控制器的动作。我已经尝试了很多组合,但似乎无法使用它。
部分视图旨在具有用于过滤 HTML 表格的文本框和按钮。
部分(“_ProfileList.cshtml”)
@model MyApp.Models.MessagingModels.ViewModels.CreateMessageViewModel
@{
var altLine = false;
}
<div>
@using (Ajax.BeginForm("Search", "Messages", new AjaxOptions { UpdateTargetId = "dvProducts", HttpMethod = "Post" }))
{
<label for="Searchbox">Find Profile :</label>
@Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
<input class="ui-state-default ui-corner-all" type="submit" value="Search" />
}
<br />
<br />
</div>
<div id="dvProducts" class="datagrid">
<table>
<thead>
<tr>
<th> </th>
<th>Profile Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.ListOfProfiles)
{
<tr class='@(altLine ? "alt" : "norm")'>
<td>
<input type="radio" name="SelectedProfile" value="@item.ID" id="SelectedProfile_@item.ID" /></td>
<td>
@if (item.Favourite)
{
<img src="@Url.Content("~/Images/favourite-star.png")" alt="Favourite" />
}
@Html.DisplayFor(modelItem => item.Name)</td>
<td>@Html.DisplayFor(modelItem => item.Description)</td>
</tr>
}
</tbody>
<tfoot>
</tfoot>
</table>
</div>
创建消息视图模型
public class CreateMessageViewModel
{
private List<ProfileModels.DataModels.Profile> _listOfProfiles;
public List<ProfileModels.DataModels.Profile> ListOfProfiles
{
get
{
using(var ctx = new EFDataContext())
{
var tmp = ctx.Profiles.OrderBy(p => p.Name).ToList();
//_listOfProfiles = ctx.Profiles.OrderBy(p => p.Name).ToList().ToPagedList(1, 10);
return _listOfProfiles;
}
}
set { _listOfProfiles = value; }
}
//..removed for brevity
}
消息视图
@model MyApp.Models.MessagingModels.ViewModels.CreateMessageViewModel
@{
ViewBag.Title = "Create";
}
<div class="grid_12">
@using (Html.BeginForm())
{
<div id="SignupForm">
<fieldset>
<legend>Profile</legend>
<strong>What profile would you like to target?</strong>
@{ Html.RenderPartial("_ProfilesList", Model); }
</fieldset>
<fieldset>
<legend>Region</legend>
</fieldset>
<fieldset>
<legend>Message</legend>
<strong>What type of message do you want to send?</strong><br />
<div id="message-type-selector">
<input type="radio" name="messagetype" value="SMS" id="message-type-sms" checked="checked" />
<label for="message-type-sms">SMS</label><br />
<input type="radio" name="messagetype" value="Email" id="message-type-email" />
<label for="message-type-email">Email</label><br />
</div>
<br />
<div id="Message-Options-SMS">
@Html.LabelFor(model => model.SMSMessageText)
<br />
@Html.EditorFor(model => model.SMSMessageText)
<div id="smscharactersremaining">100 characters remaining</div>
<button id="check-textarea">
Check Spelling
</button>
<span class="loading">loading..</span>
</div>
<div id="Message-Options-Email">
@Html.LabelFor(model => model.EmailMessageText)
<br />
<textarea id="EmailMessageText" name="EmailMessageText" cols="50" rows="15"></textarea>
<link href="@Url.Content("~/content/jHtmlArea.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jHtmlArea-0.7.5.min.js")" type="text/javascript" ></script>
<link href="@Url.Content("~/content/jHtmlArea.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/jHtmlArea.ColorPickerMenu.css")" rel="stylesheet" />
<script src="@Url.Content("~/Scripts/jHtmlArea.ColorPickerMenu-0.7.0.min.js")" type="text/javascript" ></script>
</div>
</fieldset>
<fieldset>
<legend>Tracking</legend>
</fieldset>
<fieldset>
<legend>Confirmation</legend>
</fieldset>
<p>@Html.ValidationSummary(false)
<input id="SaveAccount" type="button" value="Submit form" />
</p>
</div>
}
</div>
<div class="clear"></div>
消息控制器
public class MessagesController : Controller
{
private readonly EFDataContext db = new EFDataContext();
public ActionResult Search(string CurrentFilter)
{
var model = from p in db.Profiles
where p.Name.Contains(CurrentFilter) || p.Description.Contains(CurrentFilter)
select p;
var vmm = new CreateMessageViewModel
{
ListOfProfiles = model.ToList()
};
return PartialView("_ProfilesList", vmm);
}
}