dimanche 24 avril 2016

Want to use same model twice on same view with different type?

Currently I am working with asp mvc view I want to render data on one view...that have display data and create form on same view but it can not be done..because controller return IEnumerable type data and create form's editorfor does not contain definition for that.

I have one model "T4.Models.Order"

if i use this model as @model IEnumerable<T4.Models.Order>

create form razor syntax shows error

if i use this model as @model T4.Models.Order

display data show's error

Here is my code

@model IEnumerable<T4.Models.Order>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table table-striped table-bordered" cellspacing="0" width="100%">
     <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.CustomerID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.CustomerID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.OrderDate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.RequiredDate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShippedDate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShipVia)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Freight)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShipName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShipAddress)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShipCity)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShipRegion)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShipPostalCode)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShipCountry)
            </th>

        </tr>
    </thead>
        @*@{
            IEnumerable<T4.Models.Order> m = @model;
        }*@
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.CustomerID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.EmployeeID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.OrderDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.RequiredDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ShippedDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ShipVia)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Freight)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ShipName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ShipAddress)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ShipCity)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ShipRegion)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ShipPostalCode)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ShipCountry)
            </td>
        </tr>
    }

    </table>
</body>
</html>



@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Order</h4>
        <hr />
        @Html.ValidationSummary(true)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Note : I don't want to change anything on controller side.

Aucun commentaire:

Enregistrer un commentaire