asp.net mvc - Display Error message on same view -
i'm trying delete record database using mvc 2. delete function works fine there records foreign key relations don't wont them deleted , when user try delete such record want show error message on delete view without navigating view.
controller:
[httppost] public actionresult delete(int id, formcollection collection) { try { // todo: add delete logic here stockrepository rep = new stockrepository(); stock stock = rep.getstock(id); rep.delete(stock); rep.save(); return redirecttoaction("index"); } catch { //need display error message if unable delete return view(); } }
view:
<h2>delete</h2> <h3>are sure want delete this?</h3> <fieldset> <legend>fields</legend> <div class="display-label">stockid</div> <div class="display-field"><%: model.stockid %></div> <div class="display-label">clientname</div> <div class="display-field"><%: model.clientname %></div> <div class="display-label">itemname</div> <div class="display-field"><%: model.itemname %></div> <div class="display-label">itemcount</div> <div class="display-field"><%: model.itemcount %></div> <div class="display-label">price</div> <div class="display-field"><%: string.format("{0:f}", model.price) %></div> <div class="display-label">otherexpences</div> <div class="display-field"><%: string.format("{0:f}", model.otherexpences) %></div> <div class="display-label">totalstockvalue</div> <div class="display-field"><%: string.format("{0:f}", model.totalstockvalue) %></div> <div class="display-label">deliverydate</div> <div class="display-field"><%: string.format("{0:d}", model.deliverydate) %></div> <div class="display-label">description</div> <div class="display-field"><%: model.description %></div> </fieldset> <% using (html.beginform()) { %> <p> <input type="submit" value="delete" /> | <%: html.actionlink("back list", "index") %> </p> <% } %>
using viewdata
view
<% if (viewdata["dberror"] != null) { %> //display viewdata dberror <% } %>
controllor
[httppost] public actionresult delete(int id, formcollection collection) { try { // todo: add delete logic here stockrepository rep = new stockrepository(); stock stock = rep.getstock(id); rep.delete(stock); rep.save(); return redirecttoaction("index"); } catch { //need display error message if unable delete **viewdata["dberror"] = "error message here";** return view(); } }
Comments
Post a Comment