Get value of a partialview in the parent view


Actually, I have two questions to ask you related to the partialview.

First, I will comment on how I show the partialview

Question One


@Html.EditorFor(x => x.Suma)

<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">Modal title</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
            <div class="modal-body">
                @{ Html.RenderPartial("PartialGrupoConexion", Model.conexionadoviewmodel); }
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button>

That is the method by which I open a modal with a partialview inside, the first question is as follows.

Is this the best method of using a partialview? (within a clear modal is)

Or would it be better to use jQuery and Ajax to insert the partialview data into the modal?

Question Two

My second problem with the partialview, is that I do not understand what would be the method to obtain information from a normal view, I will use an example of my problem trying to be interpreted in the simplest way.

In the main view ( Add.cshtml ) I have an EditorFor that uses the Sum field of the Model.

And having a partialview similar to this

@Html.EditorFor(x => x.PrimerElemento)
@Html.EditorFor(x => x.SegundoElemento)

<button type="button">Calcular</button>

I managed to return the sum of these two fields to the main view, I had some ideas to do it with JQuery, but apparently I should not use it within a PartialView

EDIT The viewmodel would be something like that

public class Principal
        public int suma { get; set; }
        public int resta { get; set; }
        public SumaPartialView SumaParcial{ get; set; }
    public class SumaPartialView
        public int valor1 { get; set; }
        public int valor2 { get; set; }
        public int suma { get; set; }

I hope I expressed myself well, thank you very much for the good vibes!

asked by Juan Salvador Portugal 16.04.2018 в 17:28

1 answer


The best method would be just sending the data through json and assigning these to the controls that you already define in the html of the popup. Instead of returning html, you return json.

As explained in the article:

CRUD Operations In MVC Using Jquery Ajax | Part-2 Create PopUp Modal With Registration Form

But the way you implement it is also valid.

From the main view you can only access the popup info using jquery , it is not done from razor .

answered by 16.04.2018 / 23:08