How to pass a value from TextBox as parameter to A

2019-07-24 02:31发布

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>

I've been trying to save 2 forms for quite a while now but no luck. This is basically my last resort.

2条回答
够拽才男人
2楼-- · 2019-07-24 03:17

If you're using view models, check out this answer: MVC sending data from View to Controller

If you're only interested in sending the data from an input to the action method without view models, you can do that as well:

View:

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

Notice the BeginForm line. The first parameter is the Action I want the data to go to, which I named Edit. The next parameter is the Controller I am using, which I named SomeController. You don't add the Controller bit to the name when you're referencing the Controller in BeginForm. The third parameter is telling the form to use the POST method when sending the data to the server.

Controller:

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

If you added more inputs (again, without a view model here), you can add them as parameters to the Edit method. This isn't really the preferred method, though. Look into using a view model. ScottGu has a nice blog post on doing what you need, using view models:

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

查看更多
Emotional °昔
3楼-- · 2019-07-24 03:18

First of all, you should go with a viewmodel oriented html file since you are using MVC (Model, View, Controller):

Create a viewModel:

public class ExampleViewModel
{
    public ExampleViewModel()
    {
    }

    public virtual string TextBoxData { get; set; }
}

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" />
}

and your controller:

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!

查看更多
登录 后发表回答