Я создал модель представления с проверкой в ней, но проверка не работает, когда я отправляю форму, ниже приведен код:
namespace Products.Models
{
[MetadataType(typeof(SampleFormViewModelMetaData))]
public partial class SampleFormViewModel
{
public SampleFormViewModel() { }
public Venue venues { get; set; }
public Accomodation accomodation { get; set; }
}
public class SampleFormViewModelMetaData
{
[Required(ErrorMessage = "*")]
public object ProductName { get; set; }
[Required(ErrorMessage = "*")]
public object ProductDescription { get; set; }
[Required(ErrorMessage = "*")]
public object ProductWebsite { get; set; }
[Required(ErrorMessage = "Tel required")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public object ProductTel { get; set; }
}
}
Просмотр
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ApexAdmin.Master" Inherits="System.Web.Mvc.ViewPage<ApexTrackDays.Models.SampleFormViewModel>" %>
<div class="editor-label">
<%: Html.LabelFor(model => model.accomodation.ProductName)%>
</div>
</td>
<td>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.accomodation.ProductName, new { style = "width:300px;" })%>
<%: Html.ValidationMessageFor(model => model.accomodation.ProductName)%>
</div>
</td>
</tr>
<tr><td colspan="2">
<div class="editor-label">
<%: Html.LabelFor(model => model.accomodation.ProductDescription)%>
</div>
</td>
</tr><tr><td colspan="2">
<div class="editor-field">
<%: Html.TextAreaFor(model => model.accomodation.ProductDescription, new { @class = "tinymce" })%>
<%: Html.ValidationMessageFor(model => model.accomodation.ProductDescription)%>
</div>
</td></tr>
<tr><td>
<div class="editor-label">
<%: Html.LabelFor(model => model.accomodation.ProductWebsite)%>
</div>
</td><td>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.accomodation.ProductWebsite, new { style = "width:300px;" })%>
<%: Html.ValidationMessageFor(model => model.accomodation.ProductWebsite)%>
</div>
</td></tr> </table>
Контроллер
[HttpPost]
public ActionResult Create(FormCollection formValues)
{
// ApextrackdaysEntities entity = new ApextrackdaysEntities();
IAccomodationTypeRepository AccomodationResp = new AccomodationTypeRepository();
ITrackRepository trackResp = new TrackRepository();
IQueryable<Object> tracks = trackResp.GetVenuesSelectlist();
ViewData["Venue"] = new SelectList(tracks, "VenueID", "Name");
Accomodation accomodation = new Accomodation();
if (TryUpdateModel(accomodation))
{
accomodation.DateAdded = DateTime.Now;
accomodation.DateModified = DateTime.Now;
accomResp.Add(accomodation);
accomResp.Save();
int AccomodationID = accomodation.ID;
int VenueID = Convert.ToInt16(formValues["Venue"]);
AccomodationType type = new AccomodationType();
type.AccomodationID = AccomodationID;
type.TrackID = VenueID;
AccomodationResp.Add(type);
AccomodationResp.Save();
return RedirectToAction("Index", new { id = accomodation.ID });
}
return View(accomodation);
}