C# html表单发布到mvc控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19697283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
html form posting to mvc controller
提问by IrishManInUSA
I am trying to set up a simple login html page, whose action is sent to mvc controller on another of my sites. I have no problem setting up the page to do the post, and in the mvc controller I have my method that reads the form post. The problem is that I am not seeing my fields from the html form in the form collection.
我正在尝试设置一个简单的登录 html 页面,其操作被发送到我另一个站点上的 mvc 控制器。我设置页面来发布帖子没有问题,并且在 mvc 控制器中,我有我的方法来读取表单帖子。问题是我没有从表单集合中的 html 表单中看到我的字段。
Is there something special that I need to do to read a form post within a mvc controller method, if so what is that?
我需要做一些特别的事情来读取 mvc 控制器方法中的表单帖子,如果是这样,那是什么?
The is the form action markup from my page
这是我页面中的表单操作标记
<form action="http://reconciliation-local.sidw.com/login/launch" method="post">
User Name <input type="text" id="username"/><br/>
Password <input type="text" id="password"/>
<input type="submit" value="launch"/>
</form>
The controller method
控制器方法
[HttpPost]
public ActionResult launch(FormCollection fc)
{
foreach (string fd in fc)
{
ViewData[fd] = fc[fd];
}
return View();
}
When I step through the controller method code, I am not seeing anything in the formcollection parameter.
当我逐步执行控制器方法代码时,我在 formcollection 参数中没有看到任何内容。
回答by ohiodoug
If you posted some code it would be much easier to help you, so please edit your question...
如果您发布了一些代码,对您的帮助会容易得多,因此请编辑您的问题...
Make sure that your form's action has the correct address, that your method is specifying POST (method="POST") and that the input fields under your form have name attributes specified.
确保您的表单操作具有正确的地址,您的方法指定了 POST (method="POST") 并且您的表单下的输入字段指定了名称属性。
On the server side, try making your only parameter a FormCollection and test that the fields in your form posted through the debugger. Perhaps your model binding isn't correct and the FormCollection will at least show you what got posted, if anything.
在服务器端,尝试将您的唯一参数设置为 FormCollection 并测试表单中的字段是否通过调试器发布。也许您的模型绑定不正确,并且 FormCollection 至少会向您显示发布的内容(如果有的话)。
These are just common issues I've seen. Your problem could be different, but we need to see what you're working with to be able to tell.
这些只是我见过的常见问题。您的问题可能有所不同,但我们需要了解您正在处理的问题才能知道。
回答by don_mega
Make sure that your FormCollection object properties for username and password are defined properly.
确保正确定义了用户名和密码的 FormCollection 对象属性。
回答by IrishManInUSA
I had to use the name attribute on the text tag, and that solved my problem, is now working like a charm.
我不得不在文本标签上使用 name 属性,这解决了我的问题,现在就像一个魅力。
回答by Eugene
Try something like this:
尝试这样的事情:
cQuery _aRec = new cQuery();
_aRec.Sqlstring = "SELECT * FROM Admins";
DataSet aDS = _aRec.SelectStatement();
DataTable aDT = aDS.Tables[0];
foreach (DataRow aDR in aDT.Rows){
if (txtAdminUsername.Text == aDR[0].ToString()){
if (txtAdminPassword.Text == aDR[1].ToString()){
Session["adminId"] = aDR[0];
Response.Redirect("Admin.aspx");
return;
}
}
}
回答by Viktor Arsovski
You have to use Ajax to do that.. Whenever you want to "submit" from client side, you should use Ajax to update the server
你必须使用 Ajax 来做到这一点......每当你想从客户端“提交”时,你应该使用 Ajax 来更新服务器
Step 1 - you redirect your Ajax call to your action, but with your list of parameters in the query-string appended
第 1 步 - 您将 Ajax 调用重定向到您的操作,但附加了查询字符串中的参数列表
$.ajax(url: url + "?" + your_query_string_parameter_list_you_want_to_pass)
Step 2 - add optional parameters to your Controller-action with the same names and types you expect to get returned by the client
第 2 步 - 将可选参数添加到您的 Controller-action 中,其名称和类型与您希望客户端返回的名称和类型相同
public ActionResult MyControllerAjaxResponseMethod(type1 para1 = null,
type2 para2 = null,
type3 para3 = null, ..)
Know that the optional parameters have to be initialized, otherwise the Action itself will always ask for those
知道可选参数必须被初始化,否则 Action 本身总是会要求那些
Here's where the "magic" happens though --> MVC will automatically convert the query-string parameters into your optional controller-parameters if they match by name
这就是“魔法”发生的地方 --> 如果查询字符串参数按名称匹配,MVC 将自动将查询字符串参数转换为可选的控制器参数
I was also looking for a good answer for this, --> i.e. - one that doesn't use q-s for that usage, but couldn't find one..
我也在为此寻找一个很好的答案,--> 即-一个不使用 qs 用于该用途的答案,但找不到一个..
Kinda makes sense you can't do it in any other way except by the url though..
有点道理,除了通过 url 之外,你不能以任何其他方式做到这一点..
回答by Dmitry
Post Html To MVC Controller
将 Html 发布到 MVC 控制器
Create HTML page with form (don't forget to reference a Jquery.js)
<form id="myform" action="rec/recieveData" method="post"> User Name <input type="text" id="username" name="UserName" /><br /> Password <input type="text" id="password" name="Password"/> <input type="submit" id="btn1" value="send" /> </form> <script> $(document).ready(function () { //get button by ID $('#btn1').submit(function () { //call a function with parameters $.ajax({ url: 'rec/recieveData', //(rec)= Controller's-name //(recieveData) = Action's method name type: 'POST', timeout: '12000', (optional 12 seconds) datatype: 'text', data: { //Get the input from Document Object Model //by their ID username: myform.username.value, password: myform.password.value, } }); }); }); </script>
使用表单创建 HTML 页面(不要忘记引用 Jquery.js)
<form id="myform" action="rec/recieveData" method="post"> User Name <input type="text" id="username" name="UserName" /><br /> Password <input type="text" id="password" name="Password"/> <input type="submit" id="btn1" value="send" /> </form> <script> $(document).ready(function () { //get button by ID $('#btn1').submit(function () { //call a function with parameters $.ajax({ url: 'rec/recieveData', //(rec)= Controller's-name //(recieveData) = Action's method name type: 'POST', timeout: '12000', (optional 12 seconds) datatype: 'text', data: { //Get the input from Document Object Model //by their ID username: myform.username.value, password: myform.password.value, } }); }); }); </script>
Then in The MVC Controller
然后在 MVC 控制器中
controller/action
| |
1. Create Controller named rec (rec/recieveData)
1. 创建名为 rec (rec/recieveData) 的控制器
- Create View named rec.cshtml
- 创建名为 rec.cshtml 的视图
Here is the controller:
这是控制器:
public class recController : Controller
{
// GET: rec
string firstname = "";
string lastname = "";
List<string> myList = new List<string>();
public ActionResult recieveData(FormCollection fc)
{
//Recieve a posted form's values from parameter fc
firstname = fc[0].ToString(); //user
lastname = fc[1].ToString(); //pass
//optional: add these values to List
myList.Add(firstname);
myList.Add(lastname);
//Importan:
//These 2 values will be return with the below view
//using ViewData[""]object...
ViewData["Username"] = myList[0];
ViewData["Password"] = myList[1];
//let's Invoke view named rec.cshtml
// Optionaly we will pass myList to the view
// as object-model parameter, it will still work without it thought
return View("rec",myList);
}
}
Here is the View:
这是视图:
@{
ViewBag.Title = "rec";
}
<h2>Hello from server</h2>
<div>
@ViewData["Username"]<br /> <!--will display a username-->
@ViewData["Password"] <!-- will display a password-->
</div>