Asp.net mvc how to transfer the full model from view to controller

I have such a table in the view

<table class='sendemailtable'>                
@if (!string.IsNullOrEmpty(Model.CustomerName))
{
<tr>
   <td style="font-size: 26px;">
   @Html.Label(string.Empty, Model.CustomerName)
   </td>
</tr>
}                   

<tr><td style="padding-top: 15px;">To:</td></tr>
<tr>
   <td>
   @Html.TextBoxFor(m => m.EmailTo)
   @Html.ValidationMessageFor(m => m.EmailTo);
   </td>
</tr>

<tr><td style="padding-top: 15px;">Subject:</td></tr>
<tr>
<td style="font-size: 22px;">
@Html.TextBoxFor(m => m.EmailBody)
</td>
</tr>

few more

<button style="..." type="submit">SEND</button>

</table>

These are not all elements from the model, it has some more identifiers that are not present in ui, have some property with only getter

    public int OfferID;
    public int SomeOfferID;
    public int CustomerID;

    #region Email

    [Required]
    [DataType(DataType.EmailAddress)]
    [RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Incorrect email")]
    public string EmailTo;

    private string emailBodyDefault;
    public string EmailBody
    {
        get
        {
            if (string.IsNullOrEmpty(emailBodyDefault))
                emailBodyDefault = string.Format("Hi,{1}please take a look. Offer ID: {0}{1}{1}Thanks", SomeOfferID, Environment.NewLine);

            return emailBodyDefault;
        }

        set { emailBodyDefault = value; }
    }

    private string emailSubject;
    [Required]
    public string EmailSubject
    {
        get
        {
            if (string.IsNullOrEmpty(emailSubject))
                emailSubject = string.Format("Offer ID: {0}", SomeOfferID);

            return emailSubject;
        }

        set { emailSubject = value; }
    }

    #endregion

I want to transfer the full model to the controller in order to send email from the controller action. It is also necessary to check the user's email and non-empty subject when the user clicks on submit. How can i do this?

+4
source share
5 answers

, , . , @Html.HiddenFor(m => m.YourPropertyName), , . - , , ?

, [] DataAnnotations, , , ModelState.Valid .

public ActionResult SubmitMyForm(MyModel model)
        {
            if (ModelState.IsValid)
            {
...

:

:

@using (Html.BeginForm())
{
    <table>
    ...
    </table>
}

- , using # @. . .

+3

, , ui,

GETTERS -

, , POST.

, -

public class MyModel1
{
    public string name { get { return "Rami"; } }
    public string email { get; set; }
}

-

public ActionResult Index()
{
    MyModel1 m = new MyModel1();
    m.email = "ramilu";
    return View(m);
}

public ActionResult submit(MyModel1 m)
{
    return null;
}

, -

@using (Html.BeginForm("submit", "New", FormMethod.Post))
{
    @Html.LabelFor(model => model.email, new { @class = "control-label col-md-2" })
    @Html.EditorFor(model => model.email)
    @Html.ValidationMessageFor(model => model.email)
    <input type="submit" value="Create" class="btn btn-default" />
}

"", , .

enter image description here

GETTERS SETTERS -

(, , - , cookie .. ). .

-

public class MyModel1
{
    public string name { get; set; }
    public string email { get; set; }
}

-

public ActionResult Index()
{
    MyModel1 m = new MyModel1();
    m.email = "ramilu";
    m.email = "email";
    return View(m);
}

public ActionResult submit(MyModel1 m)
{
    return null;
}

-

@using (Html.BeginForm("submit", "New", FormMethod.Post))
{
    @Html.LabelFor(model => model.email, new { @class = "control-label col-md-2" })
    @Html.EditorFor(model => model.email)
    @Html.ValidationMessageFor(model => model.email)
    @Html.HiddenFor(model => model.name);
    <input type="submit" value="Create" class="btn btn-default" />
}

, . -

enter image description here

, . ?

JQuery Unobstructive Validation .

ASP.Net MVC ( )

+2

, TempData []..

:

<script>
$('form').submit(function() {
@{ TempData["FullModel"] = Model; }
});
</script>

:

[HttpPost]
public void Controller()
{
    YourModel model = (YourModel) TempData["FullModel"];
}
+1

    /// Use Strongly typed view (Type @Model yourModel)
    /// Use @HTML.BeginForm and a button with type="Submit"

    [HttpPost]
    public void Controller(YourModel dodel)
    {
        // Here you will get data in model
    }
0

It is always best to use strongly typed views. Just because all the things under the form tag are tied to the corresponding model binds the asp.net html element to the model using the name attribute. therefore, if you want the whole model to be intact, pass the model to view and save everything in a hidden field under the form tag.

0
source

Source: https://habr.com/ru/post/1524474/


All Articles