2

这是我的想法,但它当然不起作用。

@{
    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 })'" />

我应该如何通过这个?控制器动作名称和参数正确。只是我不知道如何获取在文本框中输入的值...

我无法在表单中保存表单,所以有人建议了这个解决方案。代理代码将是:

<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 个表格,但没有运气。这基本上是我最后的手段。

4

2 回答 2

5

首先,您应该使用面向视图模型的 html 文件,因为您使用的是 MVC(模型、视图、控制器):

创建一个视图模型:

public class ExampleViewModel
{
    public ExampleViewModel()
    {
    }

    public virtual string TextBoxData { get; set; }
}

之后,使用 viewmodel 作为模型对您的 html 进行编码:

@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
}

希望对你有帮助!

于 2012-08-29T18:08:19.047 回答
3

如果您使用的是视图模型,请查看以下答案:MVC 从视图发送数据到控制器

如果您只想将数据从输入发送到没有视图模型的操作方法,您也可以这样做:

看法:

@using (Html.BeginForm("Edit", "Some", FormMethod.Post))
{
    <input type="text" id="myTextBox" name="myTextBox" />
    <input type="submit" value="Submit" />
}

注意 BeginForm 行。第一个参数是我希望数据转到的 Action,我将其命名为 Edit。下一个参数是我正在使用的控制器,我将其命名为 SomeController。当您在 BeginForm 中引用控制器时,您不会将控制器位添加到名称中。第三个参数告诉表单在向服务器发送数据时使用 POST 方法。

控制器:

public class SomeController
{
    [HttpPost]
    public ActionResult Edit(string myTextBox)
    {
        // Do what you want with your data here.
    }
}

如果您添加了更多输入(同样,这里没有视图模型),您可以将它们作为参数添加到 Edit 方法。不过,这并不是真正的首选方法。研究使用视图模型。ScottGu 有一篇关于使用视图模型做你需要做的事情的不错的博客文章:

http://weblogs.asp.net/scottgu/archive/2007/12/09/asp-net-mvc-framework-part-4-handling-form-edit-and-post-scenarios.aspx

于 2012-08-29T18:08:18.570 回答