[英]Razor view not sending input to controller?
I am using a razor file for my view with this post form: 我正在使用此帖子表格为我的视图使用剃刀文件:
@using (Html.BeginForm("Save", "MyEventController", FormMethod.Post))
{
<md-input-container class="md-block" flex-gt-xs="">
<label>Title</label>
<input type="text" name="title" />
</md-input-container>
<md-input-container class="md-block">
<label>Address</label>
<input type="text" name="address" id="txtAddress">
</md-input-container>
<md-button class="md-raised">
<input type="submit" value="Save" />
</md-button>
}
I want to send my input to my controller in Controllers/EventController.cs as show below: 我想将输入发送到Controllers / EventController.cs中的控制器,如下所示:
public class MyEventController : Controller
{
[HttpPost]
public void Save(FormCollection collection)
{
InputEvent y = new InputEvent();
y.title = collection["title"];
y.address = collection["address"];
}
}
The Save() methods seems to never be called. Save()方法似乎永远不会被调用。 how do i submit to Save()?
如何提交到Save()?
I am developing in Visual Studio 2015 我正在Visual Studio 2015中进行开发
Remove the text "Controller" when you specify the controller. 指定控制器时,请删除文本“ Controller”。 Change it to,
更改为
Html.BeginForm("Save", "MyEvent", FormMethod.Post)
Remove the word "Controller" from your second parameter "MyEventController". 从第二个参数“ MyEventController”中删除单词“ Controller”。 So it should be "MyEvent".
因此它应该是“ MyEvent”。
Furthermore, I would create a Model (M from MVC) and when the form is posted, instead of receiving FormCollection, it will receive a nice model instead. 此外,我将创建一个模型(MVC中的M),并在发布表单时,而不是接收FormCollection,而是接收一个不错的模型。 ASP MVC will take care of binding for you.
ASP MVC将为您进行绑定。
Also, as others have pointed out, you may want to eventually return something back to the client to specify if the form submission and processing was successful. 另外,正如其他人指出的那样,您可能最终希望将某些内容返回给客户端,以指定表单提交和处理是否成功。 Therefore, your action should not return void.
因此,您的操作不应返回无效。 Most of the time in cases such as this, you should use the PRG pattern-it is very important to use this pattern.
大多数的案件时,如这个,你应该使用PRG模式,这是非常重要的是使用这种模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.