简体   繁体   中英

How to call actionresult from the view using button(submit)?

I'm trying to call actionresult in my controller. 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.

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.)

First read this article about general rounting in ASP.NET MVC

Then read this msdn article about producing empty results

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM