简体   繁体   中英

How to get tuple values in IActionResult net core 2

I have a view with two model.

This is my cshtml code:

@model Tuple<FITSWeb.Models.Test, FITSWeb.Models.Resultat>

<div class="modal-body form-horizontal">
    <div class="row">
        <div class="col-lg-12">
            <div class="modal-header">
                <h5 class="modal-title" id="ModalLabel">Selection du résultat</h5>
            </div>
            <div class="form-group" style="padding:10px">
                <label class="control-label">Démarche</label>
                <textarea readonly rows="3" class="form-control">@Model.Item1.Demarche</textarea>

                <label class="control-label">Jeu d'entrée</label>
                <textarea readonly rows="3" class="form-control">@Model.Item1.JeuEntree</textarea>

                <label class="control-label">Résultat attendu</label>
                <textarea readonly rows="3" class="form-control">@Model.Item1.ResultatAttendu</textarea>
            </div>
            <div class="modal-body">
                Selectionner le résutat retenu pour :
            </div>
            <div class="form-group" style="padding:10px">
                <label asp-for="@Model.Item2.Commentaire" class="control-label">Commentaire</label>
                <textarea rows="3" asp-for="@Model.Item2.Commentaire" class="form-control"></textarea>
                <span asp-validation-for="@Model.Item2.Commentaire" class="text-danger"></span>
            </div>
            <div class="modal-footer">
                <input type="submit" value="Enregistrer" class="btn btn-primary mb-2" />
            </div>
        </div>
    </div>
</div>

and the cs code:

        public async Task<IActionResult> AddResult(long id)
    {
        Resultat TResultat = new Resultat();
        var test = await _context.Test.Where(m => m.Id == id).Include(i => i.Resultats).FirstOrDefaultAsync();
        if (test != null)
        {
           TResultat = await _context.Resultat.Where(m => m.Id == test.ResultatRef.Id).FirstOrDefaultAsync();
            }
            return PartialView("~/Views/Tests/_Result.cshtml", Tuple.Create<Test, Resultat>(test, TResultat));
        }
        return View();
    }

how can i get tuple values for 'test' and 'Tresultat' after submit?

        [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> AddResult(long id, [Bind("Numero,Condition,Demarche,JeuEntree,ResultatAttendu,Utilisateur,DateCreation,DateModification,EstActif,Id")] Test test,
                                                        [Bind("IdTest,IdSession,Commentaire,EtatActuel,Utilisateur,DateCreation,Id")] Resultat TResultat)
    {...}

This code don't return tuple values, and i don't find the good solution.

You can try something like this(Please note this is only a sample); your "About" csthml page:

@model Tuple<M1, M2>

<form asp-action="AddResult">
    <input  name="blah1" value="@Model.Item1.Field1" />
    <input  name="blah2" value="@Model.Item2.Field2" />
    <button type="submit">Submit</button>
</form>

The backend GET action:

public IActionResult About()
{
    ViewData["Message"] = "Your application description page.";
    var vm = new Tuple<M1, M2>(new M1(), new M2());
    return View(vm);
}

The backend POST action:

[HttpPost]
public IActionResult AddResult(MyViewModel o)
{
    return RedirectToAction(nameof(About));
}

The models:

public class MyViewModel
{
    public string Blah1 { get; set; }

    public string Blah2 { get; set; }
}

public class M1
{
    public string Field1 { get; set; }
}

public class M2
{
    public string Field2 { get; set; }
}

In my opinion, you should return only one view model object instead of Tuple in the GET action because it would give you more flexibility and its easier to maintain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM