2 votes

Get value from a partialview in parent view

in fact, I have two questions to ask related to the partialview.

First, I will discuss how it is that 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 the following.

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

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

Question Two

My second problem with respect to the partialview, is that I do not understand which would be the method to obtain information these from a normal view, I'm going to use an example of my problem trying to interpret in the simplest way.

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

And having a partialview like this

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

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

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

EDIT The viewmodel would be something like this

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 have expressed well, from already thank you very much for the good vibes!


Leandro Tuttini Points 25288

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

As explained in the article:

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

But just as the way you implement it is valid.

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


HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by: