如何从文本框传递一个值作为参数,以行动 [英] How to pass a value from TextBox as parameter to Action
问题描述
这就是我脑子里想的,但当然这是行不通的。
This is what I've had in mind but of course it doesn't work.
@{
var textBoxData = form.find('input[name="textboxList"]').val();
}
<input type="button" value="Add" title="Add" onclick="location.href='@Url.Action("Create_Add", "Controller", new { textboxList = textBoxData })'" />
我应该如何传递呢?控制器操作名称和参数是否正确。只是,我不知道如何在文本框中输入的值...
How should I pass this? Controller action name and parameter are correct. Just that I don't know how to get the value entered in textbox...
我有保存表单中的表单麻烦,所以有人建议此解决方案。代理代码是:
I have trouble with saving a form within a form, so someone suggested this solution. Proxy code would be:
<firstForm>
textboxfor Name
dropdownfor DType
If DTypeDDL value is "List" then
<secondForm>
textboxfor nameOfItem
submitSecondForm (using that method i mentioned above)
</secondForm>
End If
submitFirstForm
</firstForm>
我一直在试图挽救2形式的相当长一段时间,但现在没有运气。这基本上是我的最后一招。
I've been trying to save 2 forms for quite a while now but no luck. This is basically my last resort.
推荐答案
首先,你应该有一个视图模型面向HTML文件去,因为你正在使用MVC (模型的,视图,控制器):
First of all, you should go with a viewmodel oriented html file since you are using MVC (Model, View, Controller):
创建一个视图模型:
public class ExampleViewModel
{
public ExampleViewModel()
{
}
public virtual string TextBoxData { get; set; }
}
后,代码中使用的视图模型为模型的HTML:
After, code your html using the viewmodel as model:
@model Models.Views.ExampleViewModel
@using (Html.BeginForm())
{
<div class="editor-row">
<div class="editor-label">
@Html.LabelFor(model => model.TextBoxData)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TextBoxData)
</div>
</div>
<input type="submit" value="submit" />
}
和控制器:
public ActionResult Example()
{
ExampleViewModel model = new ExampleViewModel();
return This.View(model);
}
[HttpPost]
public ActionResult Example(ExampleViewModel model)
{
string infoEntered = model.TextBoxData;
// Do something with infoEntered
}
的东西
希望这会帮助你!
Hope this will help you!
这篇关于如何从文本框传递一个值作为参数,以行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!