为什么我的视图将空数据发布到控制器

时间:2019-08-30 01:07:27

标签: c# asp.net asp.net-mvc

我可以通过索引控制器查看列表,并且显示正常。但是,当我单击“保存”以发布数据时。它不绑定我的视图模型中的数据。参见图。下面

enter image description here

控制器-ActionResult Index()

var user = HttpContext.User as ClaimsPrincipal;

int PatientID = data_Patient.getPatientID(user.FindFirst(c => c.Type == ClaimTypes.Email).Value);

var patientDashboardSetting = db.PatientDashboardSetting.Include(p => p.DashboardContent).Include(p => p.Patient);

PatientDashboardSettingView model = new PatientDashboardSettingView();

model.PatientDashboardSettings = new List<PatientDashboardSetting (patientDashboardSetting);

保存控制器

[HttpPost]
public ActionResult SaveChanges(PatientDashboardSettingView patientDashboardSettings)
{
    return RedirectToAction("Index", "PatientDashboardSetting");
}

查看模型

public class PatientDashboardSettingView
{
   public List<PatientDashboardSetting> PatientDashboardSettings { get; set; }
}

PatientDashboard模型

public partial class PatientDashboardSetting
{
    public int ID { get; set; }

    public int PatientID { get; set; }

    public int DashboardContentID { get; set; }

    public bool IsActive { get; set; }

    public bool IsSharable { get; set; }

    public virtual DashboardContent DashboardContent { get; set; }

    public virtual Patient Patient { get; set; }
}

查看表单

 @using (Html.BeginForm("SaveChanges", "PatientDashboardSetting", FormMethod.Post))
{
    for (int i = 0; i < Model.PatientDashboardSettings.Count; i++)
    {
        <div>
            @Html.HiddenFor(modelItem => Model.PatientDashboardSettings[i].ID)
            @Html.HiddenFor(modelItem => Model.PatientDashboardSettings[i].PatientID)
            @Html.HiddenFor(modelItem => Model.PatientDashboardSettings[i].DashboardContentID)
            @Html.HiddenFor(modelItem => Model.PatientDashboardSettings[i].IsSharable)
            @Html.HiddenFor(modelItem => Model.PatientDashboardSettings[i].IsActive)
        </div>
    }
    <input type="submit" value="Submit" />
}

4 个答案:

答案 0 :(得分:0)

尝试在“查看表单”中使用它:

@Html.HiddenFor(modelItem => modelItem.PatientDashboardSettings[i].ID)

答案 1 :(得分:0)

我认为可能是因为在渲染视图时,没有将任何值分配给viewModel

model.PatientDashboardSettings = new List<PatientDashboardSetting (patientDashboardSetting);

(我看不到您的其他行,所以我认为是这样),由于它们是隐藏值,因此您无法在提交前设置值,因此它们应该为null,如果是这种情况,请尝试将一些值分配给您的Index()方法中的viewModel,然后尝试再次提交。

答案 2 :(得分:0)

您必须传递模型类型中的所有值。如果其中一个不在hiddenFor列表中,则什么都不会传递。

答案 3 :(得分:-2)

                 public class QuestionsDataModel
                    {
                        public int Id { get; set; }
                        [Required]
                        public String QuestionText{ get; set; }
                
                        public int DifficultyLevelId { get; set; }
                
                        public int CorrectAnswerIndex { get; set; }
                
                
                        public List<QuestionAnswersDataModel> QuestionAnswersDataModel { get; set; }
                }  
        ----- 
           
        
         public  class QuestionAnswersDataModel
                    {
                        public int Id { get; set; }
                
                        public int QuestionId { get; set; }
                       // [Required]
                        public String AnswerText { get; set; }
                
                        public bool IsCorrext { get; set; }
                
                
                        public QuestionsDataModel Question { get; set; }
                    }
                }
        
          ---Main View

            @model QuestionsDataModel
            
            @{
                ViewData["Title"] = "Create";
            }
            
            <h1>Create</h1>
            
            <h4>Questions</h4>
            <hr />
            <div class="container">
                <form asp-action="Create">
                    <div class="row">
                        <div class="col-md-4">
            
                            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                            <div class="form-group">
                                <label asp-for="QuestionText" class="control-label"></label>
                                <input asp-for="QuestionText" class="form-control" />
                                <span asp-validation-for="QuestionText" class="text-danger"></span>
                            </div>
                            <div class="form-group">
                                <label asp-for="DifficultyLevelId" class="control-label"></label>
                                <select asp-for="DifficultyLevelId" class="form-control" asp-items="ViewBag.DifficultyLevelId"></select>
                            </div>
            
            
                        </div>
                    </div>
                    <div class="row">
                        <h1>_uestion Answers</h1>
            
            
                        @Html.Partial("_QuestionAnswers", Model.QuestionAnswersDataModel)
            
                        <div class="form-group">
                            <input type="submit" value="Save" class="btn btn-primary" />
                        </div>
                    </div>
                </form>
        --Partial
        @model List<.QuestionAnswersDataModel>
    
    @{
        ViewData["Title"] = "_QuestionAnswers";
        List<DataModel.QuestionAnswersDataModel> list = Model.ToList();
    }
    
    <h1>_uestion Answers</h1>
    
    
    <table class="table">
        <thead>
            <tr>
                <th>
                  
                </th>
                <th>
                   
                </th>
                <th>
                   AnswerText
                </th>
                <th>
                 IsCorrect
                </th>
                
            </tr>
        </thead>
        <tbody>
            @{
                int i = 0;
            }
    @for (i = 0;i< Model.Count();i++)
    {
            <tr>
                <td style="visibility:hidden">
                    <input type="hidden"asp-for="@Model[i].Id" name="QuestionAnswersDataModel[@i].Id"  />
<!--The keypoint of the answer QuestionAnswersDataModel:same name of ienumerable property in first class (i.e questionsDataModel)-->
                </td>
                <td style="visibility:hidden">
                    <input type="hidden" asp-for="@Model[i].QuestionId" name="QuestionAnswersDataModel[@i].QuestionId" />
                </td>
    
                <td>
                    @*@Html.TextBoxFor(modelItem => item.AnswerText)*@
                    <input type="text" asp-for="@Model[i].AnswerText" name="QuestionAnswersDataModel[@i].AnswerText"  />
                    @Html.ValidationMessageFor(modelItem => Model[i].AnswerText)
                </td>
                <td>
                    @Html.RadioButton("IsCorrext", Model[i].IsCorrext, new { name = "rbCorectAnswer" })
                </td>
    
            </tr>
              
                    
                        
    
    }
        </tbody>
    </table>