我正在编写一个Web应用程序来提交事件表单。我使用典型的@using (Html.BeginForm())
,然后在底部使用输入按钮提交模型。
我的视图和控制器代码如下
我的控制器在~/Controllers/FormsController.cs
下
我的观点位于~/Views/Forms/Default.cshtml
下面有很多代码几乎都是一样的,而且我的字符数限制已经过了,所以我花了很多钱。
基本上当我按下按钮上的“提交表单”按钮时,它不会转到操作方法。什么都没发生。当我用以下代替它时:
<input type="submit" value="Submit Form" onclick="location.href='@Url.Action("Save", "Forms")'" />
它有效,但我无法找到通过这种方式传递模型的方法。它只调用方法但模型为null。
所以我的最终问题是,为什么我的按钮不工作?每次按下按钮,我都会在Chrome的开发者工具中收到这些错误。所以看起来我的jquery搞砸了什么。
查看:
@model YMCA_Incident_Reports.Models.DefaultIncident
@{
ViewBag.Title = "Default Incident Form";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div>
<h1>YMCA of Burlington and Camden Counties</h1>
<p>Check appropriate Boxes</p>
</div>
@using (Html.BeginForm("Save", "Forms"))
{
<div class="table-bordered">
<table>
<thead>
<tr>
....
</tr>
</thead>
<tr>
<td>@Html.CheckBoxFor(model => model.Accident)</td>
<td>@Html.DisplayNameFor(model => model.Accident)</td>
<td>@Html.CheckBoxFor(model => model.Incident)</td>
<td>@Html.DisplayNameFor(model => model.Incident)</td>
</tr>
<tr>
<td>@Html.CheckBoxFor(model => model.MountLaurel)</td>
<td>@Html.DisplayNameFor(model => model.MountLaurel)</td>
<td>@Html.CheckBoxFor(model => model.Riverfront)</td>
<td>@Html.DisplayNameFor(model => model.Riverfront)</td>
<td>@Html.CheckBoxFor(model => model.ChildCare)</td>
<td>@Html.DisplayNameFor(model => model.ChildCare)</td>
</tr>
<tr>
<td>@Html.CheckBoxFor(model => model.PrimeTime)</td>
<td>@Html.DisplayNameFor(model => model.PrimeTime)</td>
<td>@Html.CheckBoxFor(model => model.DayCamp)</td>
<td>@Html.DisplayNameFor(model => model.DayCamp)</td>
<td>@Html.CheckBoxFor(model => model.CamdenExpansion)</td>
<td>@Html.DisplayNameFor(model => model.CamdenExpansion)</td>
</tr>
</table>
**More tables with other Html Helpers.**
<input type="submit" class="btn btn-primary" value="Submit Form" />
</div>
}
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YMCA_Incident_Reports.Models;
namespace YMCA_Incident_Reports.Controllers
{
public class FormsController : Controller
{
// GET: Forms
public ActionResult Default()
{
DefaultIncident model = new DefaultIncident();
var now = DateTime.Now;
model.TimeAndDateOfIncident = now;
return View(model);
}
// Post: Forms
public ActionResult Save(DefaultIncident model)
{
return View();
}
}
}
型号:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace YMCA_Incident_Reports.Models
{
public class DefaultIncident
{
public bool Accident { get; set; }
public bool Incident { get; set; }
[Display(Name = "Mt. Laurel")]
public bool MountLaurel { get; set; }
public bool Riverfront { get; set; }
[Display(Name = "Child Care")]
public bool ChildCare { get; set; }
[Display(Name = "Prime Time")]
public bool PrimeTime { get; set; }
[Display(Name = "Day Camp")]
public bool DayCamp { get; set; }
[Display(Name = "Camden Expansion")]
public bool CamdenExpansion { get; set; }
public string SiteLocation { get; set; }
public string Name { get; set; }
[Required(ErrorMessage = "Your must provide a PhoneNumber")]
[Display(Name = "Home Phone")]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Not a valid Phone number")]
public string PhoneNumber { get; set; }
public string DateOfBirth { get; set; }
public int? Age { get; set; }
public string Gender { get; set; }
public string GuardiansName { get; set; }
public string Address { get; set; }
public DateTime TimeAndDateOfIncident { get; set; }
public string IncidentDescription { get; set; }
public string ResolutionDescription { get; set;}
//incident types
public bool Argument { get; set; }
public bool Language { get; set; }
public bool Complaint { get; set; }
[Display(Name = "Rule Enforcment")]
public bool RuleEnforcment { get; set; }
public bool Theft { get; set; }
[Display(Name = "Fight/Bullying")]
public bool FightOrBullying { get; set; }
[Display(Name = "Water Resuce")]
public bool WaterRescue { get; set; }
[Display(Name = "Innapropriate Behavior/Abuse")]
public bool InnapropriateBehaviorOrAbuse { get; set; }
//type of injury
public bool Ache { get; set; }
public bool Bite { get; set; }
[Display(Name = "Bitten By Animal")]
public bool BittenByAnimal { get; set; }
public bool Bleeding { get; set; }
[Display(Name = "Breathing Rapidly")]
public bool BreathingRapidly { get; set; }
[Display(Name = "Breathing Shallow")]
public bool BreathingShallow { get; set; }
[Display(Name = "Broken Bone Suspected")]
public bool BrokenBoneSuspected { get; set; }
public bool Bruise { get; set; }
public bool Burn { get; set; }
public bool Chocking { get; set; }
public bool Cut { get; set; }
public bool Drowning { get; set; }
[Display(Name = "Near Drowning")]
public bool NearDrowning { get; set; }
[Display(Name = "Eye Injury")]
public bool EyeInjury { get; set; }
[Display(Name = "Foreign Object in Eye")]
public bool ForeignObjectInEye { get; set; }
[Display(Name = "Head Injury")]
public bool HeadInjury { get; set; }
public bool Itching { get; set; }
[Display(Name = "Nausea/Throw Up")]
public bool NauseaOrThrowUp { get; set; }
[Display(Name= "Nose Bleed")]
public bool NoseBleed { get; set; }
public bool Poisoning { get; set; }
public bool Rash { get; set; }
public bool Redness { get; set; }
public bool Scrape { get; set; }
public bool Scratch { get; set; }
public bool Splinter { get; set; }
public bool Sprain { get; set; }
public bool Sting { get; set; }
public bool Swelling { get; set; }
[Display(Name = "Slip/Fall")]
public bool SlipOrFall { get; set; }
[Display(Name="Other (Describe):")]
public bool OtherTypeOfInjury { get; set; }
public string OtherTypeOfInjuryDescription { get; set; }
//place on body where it occured
public bool LeftSide { get; set; }
public bool RightSide { get; set; }
public bool Abdomen { get; set; }
public bool Arm { get; set; }
public bool Ankle { get; set; }
public bool Back { get; set; }
public bool Buttocks { get; set; }
public bool Cheek { get; set; }
public bool Chest { get; set; }
public bool Chin { get; set; }
public bool Ear { get; set; }
public bool Eye { get; set; }
public bool Elbow { get; set; }
public bool Finger { get; set; }
public bool Foot { get; set; }
public bool Forehead { get; set; }
public bool Groin { get; set; }
public bool Hand { get; set; }
public bool Head { get; set; }
public bool Hip { get; set; }
public bool Knee { get; set; }
public bool Leg { get; set; }
public bool Lip { get; set; }
public bool Mouth { get; set; }
public bool Neck { get; set; }
public bool Nose { get; set; }
public bool Shoulder { get; set; }
public bool Teeth { get; set; }
public bool Thigh { get; set; }
public bool Toe { get; set; }
public bool Tongue { get; set; }
public bool Wrist { get; set; }
//Place where incident occured
public bool Classroom { get; set; }
public bool Playground { get; set; }
[Display(Name = "Fitness Center")]
public bool FitnessCenter { get; set; }
public bool Bathroom { get; set; }
[Display(Name = "Weight Room")]
public bool WeightRoom { get; set; }
[Display(Name = "Parking Lot")]
public bool ParkingLot { get; set; }
[Display(Name = "Adult Locker RM - M")]
public bool AdultLockerRoomMen { get; set; }
[Display(Name = "Adult Locker RM - W")]
public bool AdultLockerRoomWomen { get; set; }
public bool Babysitting { get; set; }
public bool Kitchen { get; set; }
[Display(Name="Side Walk")]
public bool SideWalk { get; set; }
[Display(Name = "Family Locker RM - M")]
public bool FamilyLockerRoomMen { get; set; }
[Display(Name = "Family Locker RM - W")]
public bool FamilyockerRoomWomen { get; set; }
public bool Studio { get; set; }
public bool Hallway { get; set; }
public bool Bus { get; set; }
public bool Car { get; set; }
[Display(Name = "Pool Area")]
public bool PoolArea { get; set; }
[Display(Name="Stretch Room")]
public bool StretchRoom { get; set; }
public bool Stairway { get; set; }
public bool Field { get; set; }
public bool Gym { get; set; }
public bool Playzone { get; set; }
[Display(Name = "Other (Describe):")]
public bool OtherPlace { get; set; }
public string OtherPlaceDescription { get; set; }
//type of surface
public bool Carpeting { get; set; }
public bool Rubber { get; set; }
[Display(Name = "Tile Floor")]
public bool TileFloor { get; set; }
public bool Concrete { get; set; }
[Display(Name="Wood Floor")]
public bool WoodFloor { get; set; }
public bool Asphalt { get; set; }
[Display(Name="Wood Chips")]
public bool WoodChips { get; set; }
public bool Grass { get; set; }
public bool Sand { get; set; }
[Display(Name = "Other (Describe):")]
public bool OtherTypeOfSurface { get; set; }
public string OtherTypeOfSurfaceDescription { get; set; }
//type of treatment given
[Display(Name = "Cleaned with Soap and Water")]
public bool CleanedWithSoapAndWater { get; set; }
[Display(Name = "Antiseptic Applied")]
public bool AntisepticApplied { get; set; }
[Display(Name = "Bandage Applied")]
public bool BandageApplied { get; set; }
[Display(Name = "Ice Applied")]
public bool IceApplied { get; set; }
[Display(Name = "Rest Provided")]
public bool RestProvided { get; set; }
[Display(Name = "Removed Splinter")]
public bool RemovedSplinter { get; set; }
public bool Consoled { get; set; }
[Display(Name = "Medication Given")]
public bool MedicationGiven { get; set; }
public string MedicationGivenDescription { get; set; }
[Display(Name = "Other (Describe):")]
public bool OtherTypeOfTreatment { get; set; }
public string OtherTypeOfTreatmentDescription { get; set; }
//where treatment was given
[Display(Name = "At the center")]
public bool TreatmentAtTheCenter { get; set; }
[Display(Name = "Clinic/Doctor's Office")]
public bool TreatmentAtClinicOrDoctorsOffice { get; set; }
[Display(Name = "Emergency Room")]
public bool TreatmentAtEmergencyRoom { get; set; }
//follow-up actions
[Display(Name="Parent/Emergency Contact Notified")]
public bool ParentOrEmergencyContactNotified { get; set; }
[Display(Name = "Date")]
public string DateParentOrEmergencyContactNotified { get; set; }
[Display(Name = "Time")]
public string TimeParentOrEmergencyContactNotified { get; set; }
[Display(Name = "Ambulance Called")]
public bool AmbulanceCalled { get; set; }
[Display(Name = "Poinson Control/Physician Called")]
public bool PoisonControlOrPhysicianCalled { get; set; }
[Display(Name = "Remained At Center")]
public bool RemainedAtCenter { get; set; }
[Display(Name = "Picked Up By Guardian/Drove Home")]
public bool PickedUpByGuardianOrDroveHome { get; set; }
[Display(Name = "Taken By Center Staff For Emergency Treatment")]
public bool TakenByCenterStaffForEmergencyTreatment { get; set; }
[Display(Name="Name Of Emergency Care Facility:")]
public string NameOfEmergencyCareFacility { get; set; }
[Display(Name="Condition Of Injury Upon Return To Center:")]
public string ConditionOfInjuryUponReturnToCenter { get; set; }
//witnesses
[Display(Name="Staff Supervising Program/Area")]
public string StaffSupervisingProgramOrArea { get; set; }
[Display(Name = "Staff Who Performed First Aid")]
public string StaffWhoPerformedFirstAid { get; set; }
[Display(Name = "Person(s) who witnessed the accident")]
public string WitnessesToAccident { get; set; }
//signatures
[Display(Name = "Staff Signature")]
public string StaffSignature { get; set; }
[Display(Name = "Staff Signature Date")]
public string StaffSignedDate { get; set; }
[Display(Name = "Parent Signature")]
public string ParentSignature { get; set; }
[Display(Name = "Parent Signature Date")]
public string ParentSignatureDate { get; set; }
[Display(Name = "Director Signature")]
public string DirectorSignature { get; set; }
[Display(Name = "Director Signature Date")]
public string DirectorSignatureDate { get; set; }
}
}
答案 0 :(得分:0)
可能会发生一些事情。根据您的错误,看起来验证库正在尝试验证您的表单,并且遇到错误。您可以使用novalidate
属性禁用验证:
<form ... novalidate>
此外,您的表单正在执行POST操作,但您的操作正在进行GET操作。将HttpPost
属性添加到Save
操作:
[HttpPost]
public ActionResult Save(DefaultIncident model)
{
return View();
}
没有它MVC认为Save
默认情况下它是一个GET方法。这就是你的location.href
有效的原因(当你改变href属性时,每次都会触发一个GET动作)
答案 1 :(得分:0)
我建议明确指定表单类型。所以视图的表单声明看起来像这样:
Html.BeginForm("Save", "Forms", FormMethod.Post)
然后你的控制器看起来像这样:
[HttpPost]
public ActionResult Save(DefaultIncident model)
{
return View();
}
或者,您可以将两个动词更改为GET,如果这是您要完成的目的。
答案 2 :(得分:0)
我不得不为原来隐藏的字段制作文本框。没有意识到他们是必需的(复制和粘贴问题)。一旦我拿走了必需的属性,按钮就会工作并传递数据!