Search code examples
asp.net-mvcasp.net-mvc-2strongly-typed-view

How to display edit and details on a single view in mvc


I am looking for a partially edit my model - the scenario is i want to display 5 fields and ,2 fields should be editable, and when post happens it should update the editable fields, Strongly type view provide either fully editable view or detail view , how can i have both in conjunction . Any advice or help will be appreciated.

 <tr><td>Booking ID</td><td><%: Model.ID %></td></tr>
                    <tr><td>Transaction No.</td><td><%: Model.TransactionNumber %>&nbsp;(<%: Model.PaymentProvider %>)</td></tr>
                    <tr><td>Date</td><td><%: String.Format("{0:g}", Model.DateAdded) %></td></tr>
                    <tr><td>Name</td><td><%: ViewBag.account.FirstName %>&nbsp;<%: ViewBag.account.LastName %></td></tr>
                    <tr>
                        <td>
                            <div class="editor-label">
                                <%: Html.Label("Event") %>
                            </div>
                        </td>
                        <td>
                            <div class="editor-field">
                                <%: Model.Event.Name %><%: Model.Event.Description %>
                            </div>
                        </td>
                    </tr>
                    <tr><td valign="top">Address</td><td><%= HtmlFormatting.FormatAddress(ViewBag.address)%></td></tr>
                    <tr><td>Cost</td><td>&pound;<%: String.Format("{0:F}", Model.Cost) %></td></tr>
                    <tr><td>Status</td><td><%: ViewBag.status %></td></tr>

Thnx


Solution

  • Your implementation is all wrong here. First of all, don't use <table>'s everywhere for layout, you can use the MVC template, just float the div tags left.

    You need a ViewModel within which you can reference your Booking object which I assume is a database object?

    Something like...

    public class BookingViewModel
    {
       public Booking Booking { get; set; }
    }
    

    And when you call your View from your controller pass it in

    public ActionResult Index()
    {
       return View(new BookingViewModel());
    }
    

    Then you can add a Post action result to your controller within which you can update your properties

    [HttpPost]
    public ActionResult Index(BookingViewModel model)
    {
       //Update your properties
       return View(model);
    }