[英]How to call actionresult from the view using button(submit)?
I'm trying to call actionresult in my controller. 我正在尝试在控制器中调用actionresult。 But it doesn't call it from the view.
但这并不是从视图中调用它。
I have tried using onclick="location.href='@Url.Action("action", "controller")'">
but it doesn't even enter the function. 我已经尝试过使用
onclick="location.href='@Url.Action("action", "controller")'">
但它甚至没有输入该函数。
My view: 我的观点:
@using (Html.BeginForm("WritePost", "Post", FormMethod.Post, new { enctype
= "Multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Skriv inlägg</h4>
<hr />
<div>
@{Html.RenderAction("_CategoryPartialView", "Category");}
@Html.HiddenFor(model => model.CategoryId)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new {
@class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class =
"text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Content, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Content, 10, 100, new { @class =
"form-control" })
@Html.ValidationMessageFor(model => model.Content, "", new { @class =
"text-danger" })
</div>
<input type="file" name="file" />
@Html.ValidationMessageFor(model => model.File)
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Send" class="btn btn-default" />
My Controller 我的控制器
public ActionResult EmailWhenPost()
{
try
{
if (ModelState.IsValid)
{
var senderEmail = new MailAddress("ExampleMail@gmail.com");
var receiverEmail = new MailAddress("ExampleMail2@gmail.com");
var password = "Pass";
var subject = "New Post";
var body = "Message";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(senderEmail.Address, password)
};
using (var mess = new MailMessage(senderEmail, receiverEmail)
{
Subject = subject,
Body = body
})
{
smtp.Send(mess);
}
return View();
}
}
catch (Exception)
{
ViewBag.Error = "Some Error";
}
return View();
}
I want to use the function to send an email when a post is sent. 我想使用该功能在发送帖子时发送电子邮件。 It should not return a view, just trigger the function.
它不应该返回视图,而只是触发函数。
You should call the correct function MyController/EmailWhenPost (please rename this to SendEmail, and add an attribute of [HttpPost] and then in the controller action just return void (a controller method that returns void will produce an EmptyResult.) 您应该调用正确的函数MyController / EmailWhenPost(请将其重命名为SendEmail,并添加[HttpPost]的属性,然后在控制器操作中仅返回void(返回void的控制器方法将产生EmptyResult)。
First read this article about general rounting in ASP.NET MVC 首先阅读有关ASP.NET MVC中常规漫游的文章
Then read this msdn article about producing empty results 然后阅读这篇有关产生空结果的msdn文章
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.