2017-01-23 5 views
-1

私はインシデントフォームを提出するウェブアプリを作成しています。私は典型的な@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が何かを台無しにしているように見える。

  1. name = 'MedicationGivenDescription'の無効なフォームコントロールはフォーカスできません。
  2. 名=「OtherTypeOfTreatmentDescription」が無効なフォームコントロールがフォーカス可能ではありません。

ビュー:

@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

送信ボタンの 'onclick =" .. "'を削除します。つまり、getメソッドにリダイレクトします(フォームを送信しません)。そして 'Save()'メソッドに '[HttpPost]'を追加する必要があります –

+0

私の送信ボタンに 'onclick =" .. "'がありません。また、以下の提案に従って '[HttpPost]'属性を試しましたが、うまくいきませんでした。 – Chris

+0

あなたはコードの最初のスニペットで行います:そしてあなたは '[HttpPost]'を追加しなければなりません。そして、あなたが他のコードを持っていなければコードは動作します。 –

答えて

0

元々隠しフィールドのテキストボックスを必要としないようにする必要がありました。彼らが必要とされていることを認識していませんでした(コピー&ペーストの問題)。必要な属性を取り除くと、ボタンが機能し、データが渡されました。

+1

...あなたのフォームに@ Html.ValidationSummaryがないので、エラーは見えなくなりましたが、依然としてフォームの投稿ができませんでしたか? ... RequiredAttributesを取り除きたくない場合は、必須フィールドをフォームの隠れた入力として必須ではなくで修正できますか? –

0

何か起こっている可能性があります。あなたのエラーに基づいて、検証ライブラリがフォームを検証しようとしているようで、エラーが発生しています。あなたはnovalidate属性を使用して検証を無効にすることができます

<form ... novalidate> 

また、フォームは、POSTアクションを実行しているが、あなたの行動は、GETアクションを期待しています。あなたのSaveアクションにHttpPost attibute追加します。それがなければ

[HttpPost] 
public ActionResult Save(DefaultIncident model) 
{ 
    return View(); 
} 

をMVCはそれがデフォルトでGETメソッドであるSaveを考えています。あなたのlocation.href作品(あなたはhref属性を変更するとき、それはGETアクションを毎回トリガー)

+0

私はこれもまた前にもこれを試しました。仕事をしません。おそらく私は 'Html.BeginForm( "Save"、 "Forms")にもPOSTを追加する必要がありますか?しかし、私はそれがデフォルトでPOSTだと思った。だからわからない。 – Chris

+0

その場合、レンダリングされたHTMLをフォームから投稿できますか?また、DefaultIncidentモデルはどのように見えますか?最後に、サーバーからどのようなエラーメッセージが返ってくるのですか(デバッグツールを使用) –

+0

私はサーバーからエラーを返さないようにしています。真剣に、誰も。モデルで投稿を編集します。 – Chris

0

私は明示的にフォームタイプを指定する推薦理由です。だから、ビューのフォームの宣言は次のようになります:

Html.BeginForm("Save", "Forms", FormMethod.Post) 

次に、あなたのコントローラは、このようになります。

また
[HttpPost] 
public ActionResult Save(DefaultIncident model) 
{ 
    return View(); 
} 

、あなたはそれはあなたが探しているものであればGETするには、両方の動詞を変更することができます代わりに達成する。

+0

私はこれを試しました、そのような運はありません。他の提案に対する私のコメントに従って、私はこれが事実かもしれないと思ったが、それはPOSTへのデフォルトであり、明示的に言っても、それはまだやっていない。フォームからモデルに更新された値を取得する必要があるため、投稿が必要です。 – Chris

関連する問題