I’ve a problem with my ASP.NET application when I try to pass a string from a view to a controller…
My controller (needed method)
`public ActionResult Mail(int id) //Display the View "Mail"
{
Customer customer = db.Customers.Find(id);
return View(customer);
}
[HttpPost,ActionName("Mail")] // this Method allow to send a email
public ActionResult MailConfirmed(int id)
{
Customer customer = db.Customers.Find(id);
string message = Request.Form.Get("messageBody");//Here I try to recuperate my messageBody
if(message!=null)
{
System.Diagnostics.Debug.WriteLine(message);
SendMail(customer.Mail, customer.Name, message);
return RedirectToAction("Index");
}
else
{
ModelState.AddModelError("", "Veuillez rentrer un message SVP");
}
return View(customer);
}`
View “Mail” (Page where the manager can enter a message body and click on “mail” to send the message)
'@using (Html.BeginForm())
{
<fieldset>
<legend>Customer</legend>
<div class="display-label">Pin</div>
<div class="display-field">
@Html.DisplayFor(model => model.Pin)
</div>
<div class="display-label">Name</div>
<div class="display-field">
@Html.DisplayFor(model => model.Name)
</div>
etc...
<div class="display-field">
@Html.TextBox("messageBody",null)//here the value that I try to pass in my controller
</div>
</fieldset>
}
@using (Html.BeginForm())
{
<p>
<input type="submit" value="Mail" />
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</p>
}`
the “message” is always null…Can you help me, please ?
Change your controller method like this:
Also you are passing null from “messageBody” field. Change null to “message is this” and see if it’s working.