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; }
}
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; }
}
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.