如何在浏览器MVC中键入带有字符串参数输入的控制器 [英] How to type in a controller with parameter input with string in the browser MVC

查看:55
本文介绍了如何在浏览器MVC中键入带有字符串参数输入的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带字符串输入作为参数的控制器,我怎么能从浏览器调用控制器,我试过这个:





[ ^ ]





但是我无法找到REsource。



这里是控制器:







  public   string  ReverseTheString( string  input)
{
char [] arr = input.ToCharArray ();
Array.Reverse(arr);
return new string ( ARR);
}





我尝试过:
$ b来自 $ b

AJAX使用jQuery调用ASP.NET MVC操作方法
[ ^ ]

解决方案

试试这样

 


.ajax({
type: POST
url:' ./ ControllerName / ActionName'
data:{},
dataType: json
async: true
成功:功能 (结果){
alert(result);
}
});



请参考 jQuery .ajax()| jQuery API文档 [ ^ ]


非常非常简单



如果您将此代码粘贴到家庭控制器中,并且您的iis正在侦听http:// localhost:54371然后它是


 http:// localhost:54371 / Home / ReverseTheString /?input = test 







如果你把一个空字符串检查有趣如下



 public string ReverseTheString(string input)
{
if(string.IsNullOrWhiteSpace(input))
{
returnstring is empty;
}
char [] arr = input.ToCharArray();
System.Array.Reverse(arr);
返回新字符串(arr);
}





然后再尝试这个







 http:// localhost:58455 / Home / ReverseTheString / test 









for multiple



 public string ReverseTheString(string input,string input2,int age)
{
if(string.IsNullOrWhiteSpace(input))
{
returnstring is empty;
}
char [] arr = input.ToCharArray();
System.Array.Reverse(arr);
返回新字符串(arr);
}





 http:// localhost:58455 / Home / ReverseTheString / ?输入=第一和安培;输入2 =第二&安培;年龄= 3 


i have a controller with a string input as parameter, how would i be able to call the controller from the browser, i tried it this:


[^]


but im getting the REsource cannot be found.

here is the controller:



public string ReverseTheString(string input)
     {
         char[] arr = input.ToCharArray();
         Array.Reverse(arr);
         return new string(arr);
     }



What I have tried:

from AJAX calls to ASP.NET MVC action methods using jQuery[^]

解决方案

try like this


.ajax({ type: "POST", url: './ControllerName/ActionName', data: {}, dataType: "json", async: true, success: function (result) { alert(result); } });


refer jQuery.ajax() | jQuery API Documentation[^]


Very very simple

if you have paste this code in home controller and ur iis is listening as http://localhost:54371 then it is

http://localhost:54371/Home/ReverseTheString/?input=test




interesting if you put a null string check like follows

public string ReverseTheString(string input)
      {
          if (string.IsNullOrWhiteSpace(input))
          {
              return "string is empty";
          }
          char[] arr = input.ToCharArray();
          System.Array.Reverse(arr);
          return new string(arr);
      }



then try this too



http://localhost:58455/Home/ReverseTheString/test





for multiple

public string ReverseTheString(string input, string input2, int age)
      {
          if (string.IsNullOrWhiteSpace(input))
          {
              return "string is empty";
          }
          char[] arr = input.ToCharArray();
          System.Array.Reverse(arr);
          return new string(arr);
      }



http://localhost:58455/Home/ReverseTheString/?input=first&input2=2nd&age=3


这篇关于如何在浏览器MVC中键入带有字符串参数输入的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆