I am developing an MVC 3 application and I am sending emails using MvcMailer.I am able to send basic emails, but I am trying to email the contents of a form and can't seem to work it out.
Here is the code for my model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace DFPProductions_Default.Models
{
public class Application
{
[Required]
[Display(Name = "First Name")]
public string ApplicantFirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string ApplicantLastName { get; set; }
[Display(Name = "Birth Date")]
public DateTime ApplicantBirthDate { get; set; }
[Required]
[Display(Name = "Cellphone Number")]
public string ApplicantCellphoneNumber { get; set; }
[Display(Name = "Postal Address")]
public string PostalNumber { get; set; }
[Display(Name = "Suburb")]
public string ApplicantSuburb { get; set; }
[Display(Name = "City")]
public string ApplicantCity { get; set; }
[Display(Name = "Post Code")]
public string ApplicationPostalCode { get; set; }
[Required]
[Display(Name = "Email Address")]
public string ApplicantEmailAddress { get; set; }
[Display(Name = "First Name")]
public string ParentFirstName { get; set; }
[Display(Name = "Last Name")]
public string ParentLastName { get; set; }
[Display(Name = "Email Address")]
public string ParentEmailAddress { get; set; }
[Display(Name = "Postal Address")]
public string ParentPostalNumber { get; set; }
[Display(Name = "Suburb")]
public string ParentSuburb { get; set; }
[Display(Name = "City")]
public string ParentCity {get; set;}
[Display(Name = "Post Code")]
public string ParentPostalCode {get; set;}
}
}
In my view, I have editor fields, here are a few:
<div class="editor-label">
#Html.LabelFor(model => model.ApplicantFirstName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ApplicantFirstName)
#Html.ValidationMessageFor(model => model.ApplicantFirstName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ApplicantLastName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ApplicantLastName)
#Html.ValidationMessageFor(model => model.ApplicantLastName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ApplicantBirthDate)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ApplicantBirthDate)
#Html.ValidationMessageFor(model => model.ApplicantBirthDate)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ApplicantCellphoneNumber)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ApplicantCellphoneNumber)
#Html.ValidationMessageFor(model => model.ApplicantCellphoneNumber)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ApplicantEmailAddress)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ApplicantEmailAddress)
#Html.ValidationMessageFor(model => model.ApplicantEmailAddress)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.PostalNumber)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.PostalNumber)
#Html.ValidationMessageFor(model => model.ApplicantEmailAddress)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ApplicantSuburb)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ApplicantSuburb)
#Html.ValidationMessageFor(model => model.ApplicantSuburb)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ApplicantCity)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ApplicantCity)
#Html.ValidationMessageFor(model => model.ApplicantCity)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ApplicationPostalCode)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ApplicationPostalCode)
#Html.ValidationMessageFor(model => model.ApplicationPostalCode)
</div>
</div>
I have called the email Application. The code for ApplicationMailer is:
public virtual MailMessage Application()
{
var mailMessage = new MailMessage{Subject = "Application"};
mailMessage.To.Add("debbie#gmail.com");
ViewBag.Data = "Debbie";
PopulateBody(mailMessage, viewName: "Application");
return mailMessage;
}
I have this in my controller:
private IApplicationMailer _applicationMailer = new ApplicationMailer();
public IApplicationMailer ApplicationMailer
{
get { return _applicationMailer; }
set { _applicationMailer = value; }
}
public ActionResult SendApplication()
{
ApplicationMailer.Application().Send();
//Send() extension method: using Mvc.Mailer
return RedirectToAction("Index");
}
And all I've added to the view of the email is
#model DFPProductions_Default.Models.Application
The email sends, so it is configured correctly, but I'm really not sure how to retrieve the values inserted into the form in the email view.
Thanks,
Amy
You need to utilize your model class data in the controller something like this:
public virtual MailMessage Application(ModelNameHere model)
{
var mailMessage = new MailMessage{Subject = "Application"};
mailMessage.To.Add("debbie#gmail.com");
//add field from your view here
string body = model.CellPhoneNumber + Model.FirstName;
ViewBag.Data = "Debbie";
PopulateBody(mailMessage, viewName: "Application", body);
return mailMessage;
}
Related
I'm implementing MVC based website using the Razor engine.
I have created the validation to be in the model.
i.e. Student validation is like this:
[MetadataType(typeof(Student.StudentMetaData))]
public partial class Student
{
internal sealed class StudentMetaData
{
[Required(ErrorMessageResourceName = "FirstNameRequired", ErrorMessageResourceType = typeof(StudentMessages))]
public object FirstName { set; get; }
[Required(ErrorMessageResourceName = "FatherNameRequired", ErrorMessageResourceType = typeof(StudentMessages))]
public object FatherName { set; get; }
}
}
The student already implements the IDataErrorInfo interface. The data fields exist in the other partial class (the partial class created by T4 file).
The problem is when I use it with client validation in Razor view using:
#Html.TextBoxFor(m => m.FatherName, new { #class = "form-control" })
#Html.ValidationMessageFor(m => m.FatherName)
it doesn't get the messages I specified in my resource files.
I have tried the same technique before with WPF and it works just fine.
I allowed the client validation in the Web.config and used this in my view:
#{
HtmlHelper.ClientValidationEnabled = true;
}
Add Name space in modal class in mvc.
using System.ComponentModel.DataAnnotations;
public class TelePhones
{
[Required (ErrorMessage = "MobileNo is required")]
public long MobileNo { get; set; }
[DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
[DataType(DataType.Date)]
public string DateOfBirth { get; set; }
[Range(15, 70, ErrorMessage = "Price must be between 15 and 70")]
public int Age { get; set; }
}
client side validation in mvc
<div class="editor-label">
#Html.LabelFor(model => model.MobileNo)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.MobileNo)
#Html.ValidationMessageFor(model => model.MobileNo)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.DateOfBirth)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.DateOfBirth)
#Html.ValidationMessageFor(model => model.DateOfBirth)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Age)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Age)
#Html.ValidationMessageFor(model => model.Age)
</div>
I have a form and using Grid.MVC to display in same page. When I try run my program, it show error : 'Model' conflicts with the declaration 'System.Web.Mvc.WebViewPage.Model'..
Here the sample of my code
This is my model
public class Course
{
[DisplayName("Customer Name")]
public string customer { get; set; }
[DisplayName("Contact")]
public string contact { get; set; }
[UIHint("DropDownList")]
[Required(ErrorMessage = "Please select the package")]
public int CoursePackageID { get; set; }
[Required]
public int CustomerID { get; set; }
[Required(ErrorMessage = "The date is required")]
[DisplayName("Date Of Register")]
public DateTime dtRegister { get; set; }
[DisplayName("Payment")]
public string payment { get; set; }
public List<CourseObject> lCourse { get; set; }
public class CourseObject
{
public int courseId { get; set; }
[DisplayName("Package")]
public string package { get; set; }
[DisplayName("Date Of Register")]
public DateTime date_register { get; set; }
[DisplayName("Payment")]
public string payment { get; set; }
}
}
And this is my CSHTML (Razor)
#model BangiProject.Models.Course
#using GridMvc.Html
#{
ViewBag.Title = "Register";
}
<h2>
Register</h2>
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<fieldset>
<legend>Course</legend>
<div class="control-label">
#Html.LabelFor(model => model.customer)
</div>
<div class="form-control-static">
#Html.DisplayFor(model => model.customer)
</div>
<div class="control-label">
#Html.LabelFor(model => model.contact)
</div>
<div class="form-control-static">
#Html.DisplayFor(model => model.contact)
</div>
<div class="editor-label">
Category :
</div>
<div class="editor-field">
#Html.DropDownListFor(Model => Model.CoursePackageID, new SelectList(ViewBag.CoursePackage as System.Collections.IEnumerable, "id", "course_name", new { #style = "drop-down" }),
"-Choose Package-", new { id = "cat" })
</div>
<div class="editor-label">
#Html.LabelFor(model => model.dtRegister)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.dtRegister)
#Html.ValidationMessageFor(model => model.dtRegister)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.payment)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.payment)
#Html.ValidationMessageFor(model => model.payment)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div class="row">
#Html.Grid(Model.lCourse).Columns(columns =>
{
columns.Add(c => c.courseId).Titled("ID")
.Sanitized(false)
.Encoded(false)
.RenderValueAs(o => Html.ActionLink("Edit", "Edit", "Merchant", new { id = o.courseId }, null).ToHtmlString());
columns.Add(c => c.package).Titled("Package").Filterable(true);
columns.Add(c => c.date_register).Titled("Date Of Register").Filterable(true);
}).WithPaging(25).Sortable(true)
</div>
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
The error show on this line
#Html.Grid(Model.lCourse).Columns
Please advice...
Usually this means that you're using the reserved "Model" keyword erroneously somewhere. In your case:
#Html.DropDownListFor(Model => Model.CoursePackageID...
Change this to:
#Html.DropDownListFor(model => model.CoursePackageID...
Try changing the line in question as below(lowercase 'm')...
#Html.Grid(model.lCourse).Columns
I have created a strongly typed view using visual studio and have added some dropdownlists for the user to select an option from a connected database table column but these dropdownlists seem to cause some issues. I keep getting the errors below and I am not sure why. These drop downs should be saving the options as a string to corresponding item record. Not sure why it is looking for a key named AssignedTo. Any suggestions? All help is much appreciated.
Error Message:
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'AssignedTo'.
Source Error:
Line 34: </div>
Line 35: <div class="editor-field">
Line 36: #Html.DropDownListFor(model => model.AssignedTo, (SelectList)ViewBag.AssignedToList)
Line 37: #Html.ValidationMessageFor(model => model.AssignedTo)
Line 38: </div>
Here is my strongly typed Create View:
#model ApIssues.Models.AP_Tasks
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm()) {
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>Issue</legend>
<div class="editor-label">
#Html.LabelFor(model => model.TaskDate)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.TaskDate)
#Html.ValidationMessageFor(model => model.TaskDate)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.InvDate)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.InvDate)
#Html.ValidationMessageFor(model => model.InvDate)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.AssignedTo)
</div>
<div class="editor-field">
#Html.DropDownListFor(model => model.AssignedTo, (SelectList)ViewBag.AssignedToList)
#Html.ValidationMessageFor(model => model.AssignedTo)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.CC)
</div>
<div class="editor-field">
#Html.DropDownListFor(model => model.CC, (SelectList)ViewBag.CcToList)
#Html.ValidationMessageFor(model => model.CC)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Whse)
</div>
<div class="editor-field">
#Html.DropDownListFor(model => model.Whse, (SelectList)ViewBag.WarehouseList)
#Html.ValidationMessageFor(model => model.Whse)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.PO)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.PO)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.FreightNo)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.FreightNo)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.VendName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.VendName)
#Html.ValidationMessageFor(model => model.VendName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ReqCompDate)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ReqCompDate)
#Html.ValidationMessageFor(model => model.ReqCompDate)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.TaskType)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.TaskType)
#Html.ValidationMessageFor(model => model.TaskType)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
Here is my model:
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.Validation;
namespace ApIssues.Models
{
using System;
using System.Collections.Generic;
public partial class AP_Tasks
{
[Required]
public int TaskID { get; set; }
[DataType(DataType.DateTime)]
[Display(Name = "Task Date")]
public Nullable<System.DateTime> TaskDate { get; set; }
[DataType(DataType.Text)]
[Display(Name = "Task Type")]
public string TaskType { get; set; }
[DataType(DataType.Text)]
[Display(Name = "Assigned By")]
public string AssignedBy { get; set; }
[DataType(DataType.Text)]
[Display(Name = "Assigned To")]
public string AssignedTo { get; set; }
[DataType(DataType.Text)]
[Display(Name = "CC")]
public string CC { get; set; }
[DataType(DataType.Text)]
[Display(Name = "Warehouse")]
public string Whse { get; set; }
[DataType(DataType.Text)]
[Display(Name = "PO #")]
public string PO { get; set; }
[DataType(DataType.Text)]
[Display(Name = "Freight #")]
public string FreightNo { get; set; }
[DataType(DataType.Text)]
[Display(Name = "Vendor Name")]
public string VendName { get; set; }
[DataType(DataType.DateTime)]
[Display(Name = "Req. Complete Date")]
public Nullable<System.DateTime> ReqCompDate { get; set; }
[DataType(DataType.DateTime)]
[Display(Name = "Due Date")]
public Nullable<System.DateTime> DueDate { get; set; }
[DataType(DataType.Text)]
[Display(Name = "Completion Date")]
public Nullable<System.DateTime> CompDate { get; set; }
[DataType(DataType.Text)]
[Display(Name = "Notes Summary")]
public string Notes { get; set; }
[DataType(DataType.DateTime)]
[Display(Name = "Invoice Date")]
public Nullable<System.DateTime> InvDate { get; set; }
[Display(Name = "Company Number")]
public Nullable<int> CoNo { get; set; }
[Display(Name = "Note Count")]
public Nullable<int> NoteCnt { get; set; }
}
}
Here is my Action:
[HttpGet]
public ActionResult Create()
{
//TODO: Add drop down lists
//TODO: Change the Cono in Query1
var nxtDb = new nxtSQLEntities();
var whses = from w in nxtDb.icsds select w.whse;
var warehouseList = new SelectList(whses);
ViewBag.WarehouseList =
warehouseList;
var userDb = new PDDAEntities1();
var query1 = from u in userDb.Users where u.Cono == 1 select new {u.UserName, u.FirstName, u.LastName};
var query2 = from gm in userDb.GroupMembers
join ug in userDb.UserGroups on gm.GroupID equals ug.GroupID
join u in userDb.Users on gm.UserName equals u.UserName
where ug.GroupName == "AP Department" || ug.GroupName == "MIS"
orderby new {u.LastName, u.FirstName}
select new {UserName = u.UserName, FirstName = u.FirstName, LastName = u.LastName};
var query3 = query1.Concat(query2);
var results = new List<string>{};
results.AddRange(query3.Select(entry => entry.UserName));
ViewBag.AssignedToList = new SelectList(results);
ViewBag.CcToList = new SelectList(results);
return View();
}
[HttpPost]
public ActionResult Create(AP_Tasks task)
{
// Save an task to the database
var db = new Accounting_AaronTestEntities();
try
{
db.AP_Tasks.Add(task);
db.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
return View();
}
return RedirectToAction("Index");
}
Anyone know why I am getting this error?
Try it this way.
#Html.DropDownListFor(model => model.AssignedTo,
new SelectList(ViewBag.AssignedToList, "Id", "AssignedTo"), "---Select Assigner---", new { ID = "ddlAssigned" })
Hope it helps.
try variants of the top two answers here:
Having difficulty using an ASP.NET MVC ViewBag and DropDownListfor
I have a Userstats model, and a user model, the Modelstate.isvalid in the controller is returning false, I think it may have something to do with the reltionship between the the two models but i'm not sure
userstats model:
public class UserStats
{
Calculator CalculateStats = new Calculator();
public ActivityLevel ActivitySelected { get;set; }
[Key]
public int ID { get; set; }
public User User { get; set; }
[DisplayName("Weight")]
[Required]
public double Weight { get; set; }
[DisplayName("Chest Measurement")]
[Required]
public double ChestMeasurement { get; set; }
[DisplayName("Hip Measurement")]
[Required]
public double HipMeasurement { get; set; }
[DisplayName("Waist Measurement")]
[Required]
public double WaistMeasurement { get; set; }
[DisplayName("Bicep Measurement")]
[Required]
public double BicepMeasurment { get; set; }
[DisplayName("Height Measurement(Inches)")]
[Required]
public double Height { get; set; }
[DisplayName("Body Fat %")]
[NotMapped]
public double BodyFat { get; set; }
[NotMapped]
public double BMI
{
get { return CalculateStats.CalculateBMI(Weight,Height); }
}
[NotMapped]
public double BMR
{
//get { return CalculateStats.CalculateBMR(user.SelectedGender, Weight, Height, user.Age); }
get { return 0; }
}
[DisplayName("Stats Log Date")]
[Required]
public DateTime StatDate { get; set; }
[DisplayName("Target Weight")]
[Required]
public double TargetWeight { get; set; }
[DisplayName("Target Date")]
[Required]
public DateTime TargetDate { get; set; }
[DisplayName("Wrist Measurement(Inches)")]
[Required]
public double WristMeasurement { get; set; }
[DisplayName("Forearm Measurement(Inches)")]
[Required]
public double ForeArm { get; set; }
[DisplayName("Daily Caloric Intake")]
[NotMapped]
public double DailyIntake { get; set; }
[DisplayName("Daily Allowance")]
[NotMapped]
public double DailyCalories { get; set; }
[DisplayName("Lean Body Mass")]
[NotMapped]
public double LeanMass { get; set; }
}
user model:
public class User
{
[Key]
public int ID { get; set; }
public virtual ICollection<UserStats> UserStats { get; set; }
[DisplayName("First Name")]
[Required]
public string FirstName { get; set; }
[DisplayName("Last Name")]
[Required]
public string LastName { get; set; }
[DisplayName("D.O.B")]
[DataType(DataType.Date)]
public DateTime DOB { get; set; }
private int _age;
[NotMapped]
public int Age
{
get { return _age; }
set
{
DateTime today = DateTime.Today;
_age = today.Year - DOB.Year;
if (DOB > today.AddYears(-_age)) _age--;
}
}
[DisplayName("Address")]
public string Address { get; set; }
[Required]
public string Email { get; set; }
[Required]
public Gender Gender { get; set; }
[DisplayName("UserName")]
public string UserName { get; set; }
public Gender SelectedGender { get; set; }
}
}
registeMale controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult RegisterMale(User u,UserStats userstats)
{
if (ModelState.IsValid)
{
var user = db.Users.SingleOrDefault(i => i.ID == u.ID);
userstats.User = user;
db.UserStats.Add(userstats);
db.SaveChanges();
return RedirectToAction("Details", "Dashboard", new { id = userstats.ID });
}
return View(userstats);
}
View:
<fieldset>
<legend>UserStats</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Weight)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Weight)
#Html.ValidationMessageFor(model => model.Weight)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ChestMeasurement)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ChestMeasurement)
#Html.ValidationMessageFor(model => model.ChestMeasurement)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.HipMeasurement)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.HipMeasurement)
#Html.ValidationMessageFor(model => model.HipMeasurement)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.WaistMeasurement)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.WaistMeasurement)
#Html.ValidationMessageFor(model => model.WaistMeasurement)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.BicepMeasurment)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.BicepMeasurment)
#Html.ValidationMessageFor(model => model.BicepMeasurment)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Height)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Height)
#Html.ValidationMessageFor(model => model.Height)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.StatDate)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.StatDate)
#Html.ValidationMessageFor(model => model.StatDate)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.TargetWeight)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.TargetWeight)
#Html.ValidationMessageFor(model => model.TargetWeight)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.TargetDate)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.TargetDate)
#Html.ValidationMessageFor(model => model.TargetDate)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.WristMeasurement)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.WristMeasurement)
#Html.ValidationMessageFor(model => model.WristMeasurement)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ForeArm)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ForeArm)
#Html.ValidationMessageFor(model => model.ForeArm)
</div>
<table>
<tr>
<td>
#Html.RadioButtonFor(model => model.ActivitySelected, Fitness_Friend.Web.Enumeration.ActivityLevel.Sedentary) Sedentary
</td>
</tr>
<tr>
<td>
#Html.RadioButtonFor(model => model.ActivitySelected, Fitness_Friend.Web.Enumeration.ActivityLevel.LightActivity) Light Activity
</td>
</tr>
<tr>
<td>
#Html.RadioButtonFor(model => model.ActivitySelected, Fitness_Friend.Web.Enumeration.ActivityLevel.Moderate) Moderate
</td>
</tr>
<tr>
<td>
#Html.RadioButtonFor(model => model.ActivitySelected, Fitness_Friend.Web.Enumeration.ActivityLevel.Active) Active
</td>
</tr>
<tr>
<td>
#Html.RadioButtonFor(model => model.ActivitySelected, Fitness_Friend.Web.Enumeration.ActivityLevel.Extra) Extra
</td>
</tr>
</table>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
It seems your models are not connected. There is no same id filed. You may add userID to UserStats. And I think your RegisterMale method expects only one model UserStats. Just rewrite it as follows
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult RegisterMale(UserStats model)
{
if (ModelState.IsValid)
{
var user = db.Users.SingleOrDefault(i => i.ID == model.userID );
db.UserStats.Add(userstats);
db.SaveChanges();
return RedirectToAction("Details", "Dashboard", new { id = userstats.ID });
}
return View(userstats);
}
I am trying to send the contents of a form with MvcMailer in my MVC 3 web application. The email sends, but it does not populate with the data from the form.
Here is the view of my form:
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<div id="section_1" class="section">
<p style="color:#e93738;display:none"></p>
<img src="/Content/1.png" id="one" class="step" alt="Step 1"/>
<h3>Personal Details of Student</h3>
<p>
<em>Please enter your personal details.</em><br />
</p>
<br />
<div class="editor-label">
#Html.LabelFor(model => model.ApplicantFirstName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ApplicantFirstName)
#Html.ValidationMessageFor(model => model.ApplicantFirstName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ApplicantLastName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ApplicantLastName)
#Html.ValidationMessageFor(model => model.ApplicantLastName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ApplicantBirthDate)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ApplicantBirthDate)
#Html.ValidationMessageFor(model => model.ApplicantBirthDate)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ApplicantCellphoneNumber)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ApplicantCellphoneNumber)
#Html.ValidationMessageFor(model => model.ApplicantCellphoneNumber)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ApplicantEmailAddress)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ApplicantEmailAddress)
#Html.ValidationMessageFor(model => model.ApplicantEmailAddress)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.PostalNumber)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.PostalNumber)
#Html.ValidationMessageFor(model => model.ApplicantEmailAddress)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ApplicantSuburb)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ApplicantSuburb)
#Html.ValidationMessageFor(model => model.ApplicantSuburb)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ApplicantCity)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ApplicantCity)
#Html.ValidationMessageFor(model => model.ApplicantCity)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ApplicationPostalCode)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ApplicationPostalCode)
#Html.ValidationMessageFor(model => model.ApplicationPostalCode)
</div>
</div>
<div id="section_2" class="section">
<img src="/Content/2.png" id="two" class="step" alt="Step 2"/>
<h3>Parent Details</h3>
<p>
<em>Please enter your parent or guardian's details.</em><br />
</p>
<div class="editor-label">
#Html.LabelFor(model => model.ParentFirstName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ParentFirstName)
#Html.ValidationMessageFor(model => model.ParentFirstName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ParentLastName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ParentLastName)
#Html.ValidationMessageFor(model => model.ParentLastName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ParentEmailAddress)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ParentEmailAddress)
#Html.ValidationMessageFor(model => model.ParentEmailAddress)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ParentPostalNumber)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ParentPostalNumber)
#Html.ValidationMessageFor(model => model.ParentPostalNumber)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ParentSuburb)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ParentSuburb)
#Html.ValidationMessageFor(model => model.ParentSuburb)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ParentCity)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ParentCity)
#Html.ValidationMessageFor(model => model.ParentCity)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ParentPostalCode)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ParentPostalCode)
#Html.ValidationMessageFor(model => model.ParentPostalCode)
</div>
</div>
<a href='#Url.Action("SendApplication", "Home")'><input type="submit" value="Submit" /></a>
}
Here is part of my controller:
private IApplicationMailer _applicationMailer = new ApplicationMailer();
public IApplicationMailer ApplicationMailer
{
get { return _applicationMailer; }
set { _applicationMailer = value; }
}
public ActionResult SendApplication(Application application)
{
ApplicationMailer.Application(application).Send();
//Send() extension method: using Mvc.Mailer
return RedirectToAction("Index");
}
Here is my ApplicationMailer.cs:
public virtual MailMessage Application(Application application)
{
var mailMessage = new MailMessage{Subject = "Application"};
mailMessage.To.Add("amecily#gmail.com");
ViewBag.Data = "Debbie";
ViewBag.FirstName = application.ApplicantFirstName;
ViewBag.LastName = application.ApplicantLastName;
PopulateBody(mailMessage, viewName: "Application");
return mailMessage;
}
My IApplicationMailer.cs:
public interface IApplicationMailer
{
MailMessage Application(Application application);
}
And my Application.cshtml:
#model DFPProductions_Default.Models.Application
Hi #ViewBag.Data
A new application has been received:
#ViewBag.FirstName
#ViewBag.LastName
EDIT:
At the top of the view containing the form, I have:
#model DFPProductions_Default.Models.Application
And the Application.cs is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace DFPProductions_Default.Models
{
public class Application
{
[Required]
[Display(Name = "First Name")]
public string ApplicantFirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string ApplicantLastName { get; set; }
[Display(Name = "Birth Date")]
public DateTime ApplicantBirthDate { get; set; }
[Required]
[Display(Name = "Cellphone Number")]
public string ApplicantCellphoneNumber { get; set; }
[Display(Name = "Postal Address")]
public string PostalNumber { get; set; }
[Display(Name = "Suburb")]
public string ApplicantSuburb { get; set; }
[Display(Name = "City")]
public string ApplicantCity { get; set; }
[Display(Name = "Post Code")]
public string ApplicationPostalCode { get; set; }
[Required]
[Display(Name = "Email Address")]
public string ApplicantEmailAddress { get; set; }
[Display(Name = "First Name")]
public string ParentFirstName { get; set; }
[Display(Name = "Last Name")]
public string ParentLastName { get; set; }
[Display(Name = "Email Address")]
public string ParentEmailAddress { get; set; }
[Display(Name = "Postal Address")]
public string ParentPostalNumber { get; set; }
[Display(Name = "Suburb")]
public string ParentSuburb { get; set; }
[Display(Name = "City")]
public string ParentCity {get; set;}
[Display(Name = "Post Code")]
public string ParentPostalCode {get; set;}
}
}
I think the problem lies here:
<a href='#Url.Action("SendApplication", "Home")'><input type="submit" value="Submit" /></a>
This won't work. Remove the tags entirely so only the input tag remains. Then assign the action and controllername to the form like this:
#Html.BeginForm("SendApplication", "Home", FormMethod.Post)