2017-10-26 5 views
0

Ich habe EDITSTORIES Teilansicht, die Daten zu UpdateStories Aktion in Stories Controller, aber es nicht veröffentlichen muss. Es trifft nicht einmal den Haltepunkt.Warum ruft meine Schaltfläche keine Aktion im Controller auf?

@using (Html.BeginForm("UpdateStories", "Stories", FormMethod.Post, new{enctype = "multipart/form-data" })) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>Stories</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     @Html.HiddenFor(model => model.ID) 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Image, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Image, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Story, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Story, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Story, "", new { @class = "text-danger" }) 
      </div> 
     </div> 


     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Update" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

Aktion:

[HttpPost] 
     public ActionResult UpdateStories(Stories st) 
     {  
      ViewBag.Grid= bo.GetAllImages(); 

      if (bo.UpdateImages(st)) 
      { 
       ViewBag.Data = "Updated successfully"; 
      } 
      else 
      { 
       ViewBag.Data = "Update failed"; 
      } 

      ViewBag.Style = "display:none";  
      return View("GetStories", st); 
     } 
    } 

Es ist in den GetStories, die die Hauptansicht ist. Es war ein langer Tag und immer noch, es wurde nicht gemacht. Bitte hilf mir dabei.

Update:

Routen:

routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Stories", action = "AddStories", id = UrlParameter.Optional } 
      ); 
      routes.MapRoute(
       name: "ShowStories", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Stories", action = "ShowStories", id = UrlParameter.Optional } 
      ); 

Update:

Ausblick: GetStories

@model HimHer.Models.Stories 

@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

@using (@Html.BeginForm("GetStories", "Stories", FormMethod.Get)) 
{ 
     @Html.AntiForgeryToken() 

    <div style="@ViewBag.Style"> 
     @{ 
      Html.RenderPartial("EditStories", Model); 
     } 


    </div> 

    <hr /> 
    var listData = (List<HimHer.Models.Stories>)ViewBag.Grid; 

    WebGrid wgImages = new WebGrid(listData, rowsPerPage: 20); 
    @wgImages.GetHtml(tableStyle: "table table-condensed table-bordered table-striped table-responsive", 
columns: wgImages.Columns(
         wgImages.Column 
         (columnName: "Image", header: "Image"), 
         wgImages.Column 
         (columnName: "Story", header: "Story"), 
         wgImages.Column 
         (columnName: "Image", header: "Download", format: (testItem) => Html.ActionLink("Download", "DownloadStories", new { filename = testItem.Image })), 
         wgImages.Column 
             (header: "Edit", format: (testitem) => Html.ActionLink("Edit", "EditStories", new { ID = testitem.ID, Story = testitem.Story, Image = testitem.Image, HiddenID = 1 })) 
        ) 
); 



} 

<h2>Index</h2> 
+0

Was ist der Statuscode für das Formular Post-Antwort? – Jasen

+0

Sie haben Ihre Routen in der Frage nicht angezeigt. Oder der Name des Controllers. – mason

+0

@mason udpated und contoller ist Geschichten –

Antwort

3

Sie Code 2 Formulare erzeugen und sie verschachtelt sind !.

<form action="/Stories/GetStories"> 
    <form action="/Stories/UpdateStories"> 
     <input type="submit" /> 
    </form> 
</form> 

Verschachtelte Formulare sind ungültig! Dies ist der Grund, wenn Sie auf die Übergabeschaltfläche von der inneren Form aus der Teilansicht klicken und die Aktionsmethode für die äußere Form übernehmen.

Sie sollten keine Formulare verschachteln. Verschieben Sie den Anruf an das RenderPartial außerhalb Ihres BeginForm Anrufs.

Mit Blick auf den Code, den Sie freigegeben haben, ist es nicht notwendig, das Formular-Tag in der Hauptansicht zu haben, da Sie keine Formulardaten haben, die Sie übermitteln müssen. Also einfach das entfernen.

Wenn Sie unbedingt ein anderes Formular in der Hauptansicht möchten, stellen Sie sicher, dass es keine verschachtelte Formularsituation erzeugt. Sie können 2 Formulare parallel in derselben Ansicht haben

@using (@Html.BeginForm("GetStories", "Stories", FormMethod.Get)) 
{ 
    <!-- Some form elements needed for this form -->  

} 

@{ Html.RenderPartial("EditStories", Model); } 
Verwandte Themen