0

I want to return from View List objects.

My View looks so:

@model IQueryable<CRM.ViewModel.Questionnaire.IndexQuestinaryViewModel>

<table class="table">
    <thead>
    </thead>
    <tbody>
        @foreach (var a in Model)
        {
            @Html.Hidden("UserId", @a.UserId)
            @Html.Hidden("QuestionId", @a.QuestionId)
        <tr>
            <td>@a.TextQuestion</td>
            @{
                if (a.TextResponses != null)
                {

                    <td><input type="text" name="TextResponses" value=@a.TextResponses /> </td>
                }
                else
                {
                    <td><input type="text" name="TextResponses" value="" /> </td>
                }
            }
        </tr>
        }
    </tbody>
</table>

Controller:

[HttpPost]
public ActionResult PostResponse(PostQuestionnaireViewModel model)
{
    //_userService.addQuestionInform(model);
    return RedirectToAction("Index");
}

model:

public class PostQuestionnaireViewModel
{
    public List<int> UserId { get; set; }
    public List<int> QuestionId { get; set; }
    public List<string> TextQuestion { get; set; }
    public List<string> TextResponses { get; set; }
}

enter image description here

when I return My view to controller I get list each elements. But I cant work with it. I want to add this element to database. I'd like return list class has this value.

UPD I did it like this:

public class PostQuestionnaireViewModel
{
    public List<PostViewModel> Post { get; set; }
}

public class PostViewModel
{
    public int UserId { get; set; }
    public int QuestionId { get; set; }
    public string TextQuestion { get; set; }
    public string TextResponses { get; set; }
}

But I got enter image description here

UPD

I have partial view and return get Model Like:

<div class="tab-pane fade" id="v-pills-Anketa" role="tabpanel" aria-labelledby="v-pills-settings-tab">
                    @using (Html.BeginForm("PostResponse", "AppUser", FormMethod.Post, new
                    { enctype = "multipart/form-data" }))
                    {
                        <div class="btn btn-space">
                            <button type="submit" class="btn btn-primary">Сохранить</button>
                        </div>
                        @Html.Action("GetResponse")
                    }
                </div>

Method get Responses users:

public IQueryable<IndexQuestinaryViewModel> GetAnswerResponse(int userid)
        {
            var GetQuestion = _questionRepositoryLazy.Value.GetAll().AsQueryable();
            var GetResponse = _responseRepositoryLazy.Value.GetQuery().Where(x => x.AppUserId == userid).AsQueryable();

            var Result = GetQuestion.GroupJoin(
                GetResponse,
                getquestion => getquestion.Id,
                getresponse => getresponse.QuestionId,
                (getquestion, getresponse)
                    => new IndexQuestinaryViewModel { TextQuestion = getquestion.TextQuestion, QuestionId = getquestion.Id, UserId = userid, Responses = getresponse.Where(sub => sub.QuestionId == getquestion.Id).DefaultIfEmpty(), TextResponses = getresponse.FirstOrDefault().Answer });

            return Result.AsQueryable();
        }


public class IndexQuestinaryViewModel
{
    public int QuestionId { get; set; }
    public int UserId { get; set; }
    public string TextQuestion { get; set; }
    public string TextResponses { get; set; }
    public IEnumerable<Response> Responses { get; set; }
}

Controller:

  [HttpGet]
    public ActionResult GetResponse()
    {
        var CurentId = User.Identity.GetUserId<int>();
        var GetAllQuestion = _userService.GetAnswerResponse(CurentId);
        return PartialView("_GetAnswerResponse", GetAllQuestion);
    }

I cant got it. What Am I doing wrong? I cant cutch it.

Boris
  • 43
  • 7
  • 1
    See example in the following post: [Model Binding to a List MVC 4](https://stackoverflow.com/a/15375949/6630084). According to your screen-shot the MVC is performing the data binding exactly that you was asking. Describe in more details sentence **_But I cant work with it_**. – Jackdaw Nov 03 '21 at 00:36
  • please check UPD – Boris Nov 03 '21 at 01:28
  • It hasn't been decided yet – Boris Nov 04 '21 at 12:06
  • it's solved. I will show you My solution – Boris Nov 05 '21 at 10:29

1 Answers1

0

if you have model

@model MyViewModel

You are able to do it like this: Simple Model

if you have

@model IQueryable<CRM.ViewModel.Questionnaire.IndexQuestinaryViewModel>

I've done it like:

@{
    int x = 0;
}
<table class="table">
    <tbody>
        @foreach (var a in Model)
        {
            var textResponse = "Post" + "[" + x + "]" + ".TextResponses";
            var textQuestion = "Post" + "[" + x + "]" + ".TextQuestion";
            @Html.Hidden("Post" + "[" + x + "]" + ".UserId", @a.UserId)
            @Html.Hidden("Post"+ "[" + x + "]"+ ".QuestionId", @a.QuestionId)
        <tr>
            <td>@a.TextQuestion</td>
            @{
                if (a.TextResponses != null)
                {
                    <td><input type="text" name=@textResponse value=@a.TextResponses /></td>
                }
                else
                {
                    <td>@Html.Editor(textResponse, @a.TextResponses)</td>
                }
            }
        </tr>
            x += 1;
        }
    </tbody>
</table>

Model:

public class PostQuestionnaireViewModel
{
    public List<PostViewModel> Post { get; set; }
}

public class PostViewModel
{
    public int UserId { get; set; }
    public int QuestionId { get; set; }
    //public string TextQuestion { get; set; }
    public string TextResponses { get; set; }
}
Boris
  • 43
  • 7