locked
Adding second model to edit view - just for delete function RRS feed

  • Question

  • User982203039 posted

    I have an edit page, all works fine, but now ! would like to add results from another database, model (a linked table that contains image information) -  and allow to delete a record from the second table, that is all, delete the record. I am not sure how or if I can add this to me edit view. How can I create this in the controller to display these records in the edit view? Once I get to that point I can create a delete button easily just now sure how to get to that point. Below is the code in my controller that display the edit information. The db  want to add is called OrderImages.

    Thanks!

    public ActionResult EditValuationOrder(int id)
      {
        var o = db.Orders.Find(id);
        return View(o);
      }

    Wednesday, May 6, 2020 9:58 PM

All replies

  • User1120430333 posted

    IMO, you are confusing the persistence model EF uses with a viewmodel. They are two models with different purposes, and one should never send a persistence model into a view. A VM is strong typed to the cshtml view, The EF persistence model populates a viewmodel,  and the results of the VM based on user interaction in the view populates the EF persistence model to be persisted to the database.

    https://www.dotnettricks.com/learn/mvc/understanding-viewmodel-in-aspnet-mvc

    You can use a VM in any view with different purposes. 

    Thursday, May 7, 2020 1:03 AM
  • User1686398519 posted

    Hi, Baze72

    Depending on your needs, you can use Tuple so that you can use two models from different database on one page.

    I make a simple example, you can refer to it.

    Controller

            private ManagerContext db = new ManagerContext();
    private ManagerContextTwo db2 = new ManagerContextTwo();
    // GET: TwoDatabase
    public ActionResult Index()
    {
    TestUser testUser = db.TestUsers.Find(1);
    ImageModel imageModel = db2.ImageModels.Find(1);
    var tuple = new Tuple<TestUser, ImageModel>(testUser, imageModel);

    return View(tuple);
    }

    Page

    @using yihuisunMVC.Models;
    
    @model Tuple<TestUser, ImageModel>
    
    @{
    
        ViewBag.Title = "Index";
    
    }
    
    <h2>Index</h2>
    
    <h2>TestUser from fist database</h2>
    
    <table class="table">
    
        <tr>
    
            <td>@Html.LabelFor(tuple => tuple.Item1.Id)</td>
    
            <td>@Html.DisplayFor(tuple => tuple.Item1.Id)</td>
    
        </tr>
    
        <tr>
    
            <td>@Html.LabelFor(tuple => tuple.Item1.Name)</td>
    
            <td>@Html.DisplayFor(tuple => tuple.Item1.Name)</td>
    
        </tr>
    
    </table>
    
    <h2>ImageModel from second database</h2>
    
    <table class="table">
    
        <tr>
    
            <td>@Html.LabelFor(tuple => tuple.Item2.Id)</td>
    
            <td>@Html.DisplayFor(tuple => tuple.Item2.Id)</td>
    
        </tr>
    
        <tr>
    
            <td>@Html.LabelFor(tuple => tuple.Item2.url)</td>
    
            <td>@Html.DisplayFor(tuple => tuple.Item2.url)</td>
    
        </tr>
    
    </table>

    Here is the result.

    Best Regards,

    YihuiSun

    Thursday, May 7, 2020 12:23 PM