Edit a dependancy

Gloops 41 Reputation points
2021-01-12T10:14:10.897+00:00

Hello everybody,

I created an MVC site to display customers' opinions about restaurants.

With MVC 5 it is very quick to obtain automatic views when creating the controllers.

So, there is a page Avis/Edit/n#, where you can edit an opinion.

On Restaurants/Details/n# I displayed the list of opinions concerning the restaurant n#.

On it I added a link to Avis/Edit/n# to edit the opinion in front of it (I shall later manage the rights), but from there, the links send "back" to the list of opinions. It would be much better to send back to the page of the restaurant.

Any hint about this ? In fact there are two ways of managing that :

  • either I use the Avis/edit page, but I call it with a parameter that tells it what links to display
  • or I write a subpage to insert in the Restaurant/Details page, to edit the opinions

The first solution is cleaner in the meaning of "do not repeat yourself", but the second one offers a nicer interface. For it I have to remember how to edit data from a sub-page, and that for an element in a list.

Is it a problem to edit data on a details page, that is initially planned to display ?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,270 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Jerry Cai-MSFT 986 Reputation points
    2021-01-13T03:23:42.16+00:00

    Hi,Gloops

    For it I have to remember how to edit data from a sub-page, and that for an element in a list.

    You can finish this by using bootstrap modal, click edit button to call the modal out, and get the data to the modal, edit in it, then submit to your controller

    and save to the database, the following is a demo:

    Controller:

    public ActionResult Index()  
            {  
                var model = _db.MyModels.ToList();  
                return View(model);  
            }  
      
            public ActionResult Edit(MyModel myModel)  
            {  
                var update = _db.MyModels.Find(myModel.Id);  
                TryUpdateModel(update, "", new string[] {"opinions" });  
                _db.SaveChanges();  
                return RedirectToAction("Index");  
            }  
    

    View:

    @默 IEnumerable<Gloops1131.Models.MyModel>

    <table class="table">  
        <tr>  
            <th>  
                @Html.DisplayNameFor(model => model.Id)  
            </th>  
            <th>  
                @Html.DisplayNameFor(model => model.opinions)  
            </th>  
            <th></th>  
        </tr>  
        @foreach (var item in Model)  
        {  
            <tr>  
                <td>  
                    @Html.DisplayFor(modelItem => item.Id)  
                </td>  
                <td>  
                    @Html.DisplayFor(modelItem => item.opinions)  
                </td>  
                <td>  
                    <button type="button"                     
                            class="btn btn-primary"  
                            data-toggle="modal"  
                            data-target="#exampleModal"  
                            data-id="@item.Id"  
                            data-opinion="@item.opinions">Edit</Button>  
                </td>  
            </tr>  
        }  
    </table>  
      
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">  
        <div class="modal-dialog" role="document">  
            <div class="modal-content">  
                <div class="modal-header">  
                    <h5 class="modal-title" id="exampleModalLabel">Edit opinions</h5>  
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">  
                        <span aria-hidden="true">&times;</span>  
                    </button>  
                </div>  
                <form method="post" action="/Home/Edit">  
                    <div class="modal-body">  
                        <input type="hidden" id="id" name="id" />  
                        <div class="form-group">  
                            <label>Opinion:</label>  
                            <input type="text" class="form-control" id="opinions" name="opinions" />  
                        </div>  
                    </div>  
                    <div class="modal-footer">  
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>  
                        <input type="submit" class="btn btn-primary" value="Save" />  
                    </div>  
                </form>  
            </div>  
        </div>  
    </div>  
    

    The Javascript in the above view:

            $('#exampleModal').on('show.bs.modal', function (event) {  
                var button = $(event.relatedTarget);  
                var id = button.data('id');  
                var opinion = button.data('opinion');  
                var modal = $(this);  
                modal.find('.modal-body input[name="id"]').val(id);  
                modal.find('.modal-body input[name="opinions"]').val(opinion);  
            });  
    

    Result:
    56008-glopp.gif

    Best Regards,
    Jerry Cai


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Gloops 41 Reputation points
    2021-01-14T09:04:34.913+00:00

    Hello,

    It works jolly good, thank you (yesterday I was fighting about a mysterious update of Windows 10).

    There is just one thing : after validating the edit I arrive on Index of opinions, rather than the details of the restaurant.

    Do I add something at the end of the Bootstrap function ?

    Oh, in fact the form action is Home/Edit. I wonder whether I should create an edit action in the Home controller. Home has three views : About, Contact, Index. But it appears the navigation does not go there after validation.
    Perhaps it would be clearer to add a second edit action in Opinions, for instance LocalEdit, that would go to the details of the restaurant when finished.


  3. Gloops 41 Reputation points
    2021-01-14T09:34:35.727+00:00

    Oh, I realize that I must have missed something, as I just display the Opinion/Edit view.

    0 comments No comments

  4. Gloops 41 Reputation points
    2021-01-14T14:13:42.073+00:00

    Hello,

    You know what ? I had got some difficulties to interpret your work.

    I work on the Details view of the Restaurant.

    The Model object is supposed to be a Restaurant, is not it ?

    If you want to create a list with

     @foreach (var item in Model)  
    

    (line 11 in the view),

    then you need to receive an enumerable.

    I see you display model.opinion, I did not pay attention to that, but the opinion is an object with an Id, a title, a text, and a score (perhaps not the best name, but it means a numerical appreciation from 1 to 5).

    I presume the list could be created with something like

    foreach(Opinion item in model.opinions)

    So, copy/paste your code as is was perhaps not too much a good idea.

    I had no error message, but it seems I did not execute what you proposed, but called Opinion/Edit instead.

    For sure what you propose will provide a nice user interface indeed, but I have got to adapt so it needs some more time than I first expected.


  5. Gloops 41 Reputation points
    2021-01-15T05:46:57.913+00:00

    (Oh, I do not see your last message any more.)
    Thank you for the proposal. Anyway I shall have to spend time for the redaction and shaping, so that it can be understandable. After trying to understand what control is used for what, if needed I shall do that.
    For the moment being, I called Opinion/Edit with a parameter, so that it returns to the calling page after validation. It is less beautiful, but quite efficient.
    Not sure I can go on with this before Sunday, I do not know how much time we can let a thread unclosed.

    0 comments No comments