简体   繁体   English

Razor 表单没有传回控制器方法

[英]Razor form not passing back to controller method

I am trying to pass a textbox's text back to a method in my controller, but the data is not being passed in the parameter I am genuinely confused, i'm following another example but i'm getting a different result, ie - my method behaving as if no parameter is passed我正在尝试将文本框的文本传递回控制器中的一个方法,但数据没有在参数中传递我真的很困惑,我正在关注另一个示例,但我得到了不同的结果,即 - 我的方法表现得好像没有传递参数

Code代码

public ActionResult Index(string searchString)        
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";

        var listOfAnimals = db.Animals.ToList();




        if (!String.IsNullOrEmpty(searchString))            
        {
            listOfAnimals = listOfAnimals.Where(a => a.AnimalName.ToLower().Contains(searchString.ToLower())).ToList();                                            

        }

        return View(listOfAnimals);
    }

and here is my razor form from my view page这是我查看页面上的剃刀表格

@using(Html.BeginForm("Index", "Home"))
{ 
     @Html.TextBox("searchString")
     <input type="submit" id="Index" value="Index" />         
} 

Can anybody spot why this isn't working?任何人都可以发现为什么这不起作用?

If more code is needed, please let me know but i think the issue is isolated to here如果需要更多代码,请告诉我,但我认为问题与此处无关

You code is correct.你的代码是正确的。

Since you didn't add [HttpGet] or [HttpPost] before index method.由于您没有在 index 方法之前添加 [HttpGet] 或 [HttpPost]。 This method was called twice.这个方法被调用了两次。

The first call ran when producing the page with form via url http://server/Home/Index .第一个调用是在通过 url http://server/Home/Index生成带有表单的页面时运行的。 This call was an http get and searchString mapped from URL was null, which is correct.这个调用是一个 http get 并且从 URL 映射的 searchString 为空,这是正确的。

The second call ran when you clicked submit button.当您单击提交按钮时,第二个调用运行。 Correct value would be mapped by MVC correctly.正确的值将由 MVC 正确映射。

You need to have 2 Index actions (two methods), one without decorations (GET verb) and another one decorated with HttpPost (POST verb).您需要有 2 个索引操作(两种方法),一个没有装饰(GET 动词),另一个用 HttpPost 装饰(POST 动词)。 Basically, when you go to the index page, the GET action is executed.基本上,当您转到索引页面时,将执行 GET 操作。 When you submit the form, a POST request is executed and the Index decorated with HttpPost is executed.当您提交表单时,将执行 POST 请求并执行用 HttpPost 装饰的索引。

// GET
public ActionResult Index() { ... }

// POST
[HttpPost]
public ActionResult Index(string searchString) { ... }

Francisco Goldenstein wrote the recommended way .弗朗西斯科·戈登斯坦写了推荐的方式 It means you can have two Index() actions:这意味着您可以有两个Index()操作:

// for GET
public ActionResult Index() { ... }

// for POST
[HttpPost]
public ActionResult Index(string searchString) { ... }

However it is possible to have one Index() method for handling both (GET and POST) requests:但是,可以使用一个Index()方法来处理(GET 和 POST)请求:

public ActionResult Index(string searchString = "") 
{
   if(!string.IsNullOrEmpty(searchString)
   { /* apply filter rule here */ }
}

You wrote, your code is not working.你写的,你的代码不起作用。 Do you mean, your action method is not requested after click on the button?您的意思是,单击按钮后不会请求您的操作方法吗? Consider to allow empty value Index(string searchString = "")考虑允许空值Index(string searchString = "")

If your action method is fired but variable is empty, check the name on the View() side.如果您的操作方法被触发但变量为空,请检查 View() 端的名称。 Textbox must not be disabled, of course.当然不能禁用文本框。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Ajax从Bootstrap模态将Razor Form数据作为模型传递回C#控制器方法 - Pass Razor Form data back to C# controller method as Model from bootstrap modal with ajax 将 IEnumerable 传递给视图以生成表单,然后将表单数据传回 controller - Passing an IEnumerable to a view to generate a form and then passing form data back to controller 将文本框的值从View传递到Controller中的方法/操作,然后将数据从响应传递回表单 - Passing a value of a textbox from View to a method/action in Controller and then data from response back to form 在Razor视图中另一个控制器的调用方法和传递参数 - in Razor view calling method of another controller and passing parameters 将多行字符串传递给 Controller 进行加密并传递回 Razor 视图 - Passing a multi-line string to Controller to be encrypted and passed back to a Razor view 从 js 文件将数组传递给 controller 中的方法并取回值 - Passing an array to a method in controller from a js file and getting back the value 使用razor中的方法post传递参数 - Passing parameters with the method post in razor 将表单添加到umbraco v6纯剃刀视图中以调用曲面控制器动作传递ID - Add form to umbraco v6 pure razor view to call surface controller action passing ID 将ReportParameters传递回方法 - Passing ReportParameters back to method MVC Razor将模型从视图传递到控制器 - MVC Razor passing Model from View to Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM