开发者

asp mvc user controls

开发者 https://www.devze.com 2023-01-27 07:36 出处:网络
I want to write user control to sending email. I write that control: <%@ Control Language=\"C#\" Inherits=\"System.Web.Mvc.Vie开发者_如何学CwUserControl\" %>

I want to write user control to sending email.

I write that control:

<%@ Control Language="C#" Inherits="System.Web.Mvc.Vie开发者_如何学CwUserControl" %>
<form action="" method="post">
<div class="box">
    <div class="box-header">
        Rejestracja</div>
    <div class="box-content">
        <div>
            Imię
        </div>
        <div>
            <input name="firstname" type="text" />
        </div>
        <div>
            Nazwisko
        </div>
        <div>
            <input name="lastname" type="text" />
        </div>
        <div>
            Email
        </div>
        <div>
            <input name="email" type="text" />
        </div>
        <div>
            Ulica nr domu mieszkania
        </div>
        <div>
            <input name="street" type="text" />
        </div>
    </div>
    <div class="box-info">
        Wypełnij formularz rejestracyjny i dołącz do klubu Oriflame.
    </div>
</div>
<div style="clear: both;">
</div>
</form>

And i put this control in masterpage:

<% Html.RenderPartial("Kontakt"); %>

That control named :kontakt.aspx" and it is in shared folder

My question is where i must write code with sending email. What action i myst set in controls form.

This control was be on all sites.

Regards


The form needs to post to a URL that is setup to route to a controller action. That could be the current page's Url or a different Url.

In your controller you want a method that accepts the form fields. This could be a FormCollection object or a strongly typed model who's properties map to the form names.

[HttpPost]
public ActionResult Foo(FormCollection form)
{
    .. use the form collection to construct your email ...          
}

If you're using a strongly typed view, rather than building the HTML inputs yourself you could do:

<%= Html.TextBoxFor(x => x.FirstName) %>

And in your controller action you can use the model rather than the FormCollection:

[HttpPost]
public ActionResult Foo(KontaktModel details)
{
    .. use the details object to construct your email ...          
}

I suggest taking a look through the tutorials at http://asp.net/mvc as well as doing the NerdDinner tutorial.


You have to create some sort of Controller that will receive form data. And you can send those emails from the server (from controller or whatever you chose to send it).


Write your email creation and sending code in a controller method. You'd then call it from this Kontakt partial view like this:

<% using (Html.BeginForm("SendMail", "Mail")) { %>

Where SendMail is the method, and Mail is the name of the controller.

public ActionResult SendMail()
{        
     //build your mail objects and send as needed.
     return View();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号