1

I have this method in my controller, it downloads a list of data from my database

[HttpPost]
public IActionResult Wyswietl()
{
    List<Uzytkownik> dupa = _uchwycik.Wypisz();

    return RedirectToAction("Index");
}

Here is my index page I'm calling partial, but main issue is how to pass the model data to it? Right now it says model is empty because obviously no data is being send. as u can see index and partial use different models, only one model is allowed per view.

@model TestUmiejetnosci.Models.Uzytkownik

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>o sesji sie nauczyc.I jak wyswietlic te dan eteraz?Zrobic formularz w zaleznosci od formularza modyfikacja i przesylanie danych do metody kontrolera i odbieranie ich <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>

    <form method="post" asp-action="Dodaj">
        <div class="form-group">
            <label asp-for="Imie">Imie</label>
            <input asp-for="Imie" class="form-control" />
            <span asp-validation-for="Imie" class="text-danger"></span>
        </div>

        <div class="form-group">
            <label asp-for="Nazwisko">Nazwisko</label>
            <input asp-for="Nazwisko" class="form-control" />
            <span asp-validation-for="Nazwisko" class="text-danger"></span>
        </div>

        <div class="form-group">
            <label asp-for="wiek">Wiek</label>
            <input asp-for="wiek" class="form-control" />
            <span asp-validation-for="wiek" class="text-danger"></span>
        </div>

        <button type="submit" class="btn btn-primary">Wyslij</button>
    </form>
</div>

<br />
<br />

<form method="post" asp-action="CheckList">
   
   @Html.Partial("_PrivacyPartial")

    <button> Pokaz liste</button>
</form>

Partial

@model List<TestUmiejetnosci.Models.Uzytkownik>

<h1> Widok privacy!!</h1>

<br />
<Br />
<br />

@foreach (var wyswietl in Model)
{
    <p> Imie </p> @wyswietl.Imie  <p> Nazwisko </p> @wyswietl.Nazwisko <p> Wiek </p> @wyswietl.wiek
    <br /> <br />
}

I tired to also send data through viewbag or tempdata but I still get an error message

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Emil6011
  • 11
  • 1

2 Answers2

1

I am little bit confused by your question mainly due to your wording, but since you are polish and english seems to be your 2nd, 3rd or perhaps even higher language, please take no offense in that. Now if you are attempting at getting a list of data out of your database and display it, here is how I do this without an issue:

Before we start, unless my mind cheats me, "HtttpPost" should not be used infront of an Index method but rather be kept for "Posting" methods which pass data to the database such as "Create" or "Edit".

controller:

First, you have to pass your dbContext into a constructor which I guess you did, but just in case.

public table_nameController(dbConext_class_name db)
{
     _db = db;
}

then the method which passes the list with the data out of the database.

public IActionResult Index()
{
    IEnumerable<model_name> model_nameList= _db;
    return view(model_nameList);
}

replace "model_name" with the name of the model from which you wish the data to be fetched.

Then your view code could look something along the lines of the following. Keep in mind that in my scenario all of my models are placed in a folder called "Models".

@model IEnumerable<namespace_name.Models.model_name>

<div class="container p-3">
    <div class="row pt-4">
        <div class="col-6">
            <h2 class="text-primary">List of XY items</h2>
        </div>     
        <div class="col-6 text-right">
            <a asp-controller="model_name" asp-action="Create" class="btn btn-primary">
                +
            </a>
        </div>
    </div>
    <br /><br />
    @if (Model.Count() > 0)
    {
        <table class="table table-bordered table-striped" style-width:100%>
            <thead>
                <tr>                    
                    <th>
                        1st_attriubute
                    </th>
                    <th>
                        2nd_attribute
                    </th>
                    <th>
                        3rd_Attribute
                    </th>
                    <th>
                        you_get_this_by_now
                    </th>                       
                </tr>
            </thead>
            <tbody>
                @foreach (var obj in Model)
                {
                    <tr>                        
                        <td width="10%">@obj.1st_attribute</td>
                        <td width="10%">@obj.2nd_attribute</td>
                        <td width="10%">@obj.3rd_attribute</td>                            
                        <td class="text-center">                                
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    }
    else
    {
        <p>No records to be displayed</p>
    }
</div>

I am answering the part of your question which seems understandable to me. The rest seems little bit "wild". But if you wish to pass further explanation, I am listening and I will do my best to help my fellow beginner.

Best of luck and I hope this helped.

0

You can put your index and partial models inside the master model and pass the master model to the Index view.

public class ModelA
{
    public override string ToString()
    {
        return "A";
    }
}

public class ModelB
{
    public override string ToString()
    {
        return "B";
    }
}

public class MasterModel
{
    public ModelA A { get; set; }
    public ModelB B { get; set; }
}

The controller action than looks as follows:

public IActionResult Index()
{
    var masterModel = new MasterModel();
    masterModel.A = new ModelA();
    masterModel.B = new ModelB();

    return View(masterModel);
}

In the Index view you can further pass model B to the partial view. The extension method IHtmlHelper.Partial may accept the model in the second argument.

@model MasterModel

<div>
    Main view: @Model.A
    @Html.Partial("Partial", Model.B)
</div>

The partial view looks as follows:

@model ModelB

<div>
    Partial view: @Model
</div>

Here is the output:

enter image description here

Alternatively, you can drop IHtmlHelper.Partial and use the MVC Core Components. The master model is not needed in this case.

Controller:

public class HomeController : Controller
{
    public IActionResult Index()
    {
        var modelA = new ModelA();
        return View(modelA);
    }
}

Component:

public class PartialViewComponent : ViewComponent
{
    public IViewComponentResult Invoke()
    {
        var modelB = new ModelB();
        return View("Partial", modelB);
    }
}

Index view:

@model ModelA

<div>
    Main view: @Model
    @await Component.InvokeAsync("Partial")
</div>

Partial view:

@model ModelB

<div>
    Partial view: @Model
</div>

The output is exactly the same in this case:

enter image description here

Sergey
  • 581
  • 1
  • 5
  • 12