我认为我有Ajax表单:
@using (Ajax.BeginForm("SearchHuman", "Search", new AjaxOptions(){
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "result" }))
{
<div class="editor-field">
@DescriptionStrings.Lastname:
@Html.TextBox("LastName")
</div>
<div class="editor-field">
@DescriptionStrings.Firstname:
@Html.TextBox("Name")
</div>
//submit button
<input type="submit" value='Start Searching' />
//submit link
@Ajax.ActionLink("search", "OtherSearch", new{lastName ="",...}, new AjaxOptions()
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "tab"
})
}
我想使用一个表单提交按钮和2个不同搜索(在不同数据库中)的链接。但是如何将路径值从表单的文本框传递到Ajax.ActionLink?
提前致谢!
答案 0 :(得分:1)
但是如何将路径值从表单的文本框传递到Ajax.ActionLink?
你做不到。如果要将值发送到服务器,则应使用提交按钮。您可以在同一表单中提交2个提交按钮,这两个按钮都提交给同一控制器操作。然后在此操作中,您可以测试单击了哪个按钮,并根据其值执行一个或另一个搜索。
示例:
<button type="submit" name="btn" value="search1">Start Searching</button>
<button type="submit" name="btn" value="search2">Some other search</button>
然后在你的控制器动作中:
[HttpPost]
public ActionResult SomeAction(string btn, MyViewModel model)
{
if (btn == "search1")
{
// the first search button was clicked
}
else if (btn == "search2")
{
// the second search button was clicked
}
...
}
答案 1 :(得分:0)
我们选择的解决方案是实现一个自定义的ActionMethodSelectorAttribute,它允许我们根据其name属性来区分按下哪个按钮。然后我们用ActionName装饰器装饰了许多方法,给它们提供了相同的动作名称(在BeginFrom帮助器中指定的那个),然后我们使用自定义的ActionMethodSelector装饰器来区分根据点击按钮的名称调用哪个方法。最终结果是每个提交按钮都会导致调用一个单独的方法。
一些代码来说明:
在控制器中:
[ActionName("RequestSubmit")]
[MyctionSelector(name = "Btn_First")]
public ActionResult FirstMethod(MyModel modelToAdd)
{
//Do whatever FirstMethod is supposed to do here
}
[ActionName("RequestSubmit")]
[MyctionSelector(name = "Btn_Second")]
public ActionResult SecondMethod(MyModel modelToAdd)
{
//Do whatever SecondMethod is supposed to do here
}
在视图中:
@using (Ajax.BeginForm("RequestSubmit",.....
<input type="submit" id="Btn_First" name="Btn_First" value="First"/>
<input type="submit" id="Btn_Second" name="Btn_Second" value="Second"/>
至于自定义属性:
public string name { get; set; }
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
var btnName = controllerContext.Controller.ValueProvider.GetValue(name);
return btnName != null;
}