控制器的POST方法中只有一个参数没有填充,为什么?

时间:2019-04-26 18:07:40

标签: c# asp.net-mvc post model-view-controller

我正在尝试通过BeginForm / TextboxFor和HTTP发布设置模型的所有三个对象。表单的前两个参数有效,但是由于某种原因,最后一个参数不起作用,并且没有用我输入到文本框中的任何值填充。

我的控制器中的ActionResult:

[HttpPost]
public ActionResult UpdateClient(HttpPostedFileBase UploadedVideo, Workout workout, WorkoutInputs workoutInputs)
{
   if (Request.IsAuthenticated)
   {
       if (User.IsInRole("Admin"))
       {
          WorkoutViewModel workoutViewModel = new WorkoutViewModel();
          workoutViewModel.Clients = GetClients();
          int client_id = workout.ClientId ?? default(int);
          workoutViewModel.Client = GetClient(client_id);

          if (UploadedVideo.ContentLength > 0)
          {
             string VideoFilename = Path.GetFileName(UploadedVideo.FileName);
             FilePath = Path.Combine(Server.MapPath("~/UploadedClientVideos"), VideoFilename);
             UploadedVideo.SaveAs(FilePath);
             ViewBag.Message = "Video and data saved successfully.";
             try
             {
                using (OPPDBContext context = new OPPDBContext())
                {
                   List<WorkoutInputs> lst = context.WorkoutInputs.ToList();

                   workoutInputs.link = "/UploadedClientVideos/" + VideoFilename;
                   workoutInputs.Id = lst[lst.Count - 1].Id + 1;
                   workoutInputs.WorkoutId = workout.workout_id;
                   context.WorkoutInputs.Add(workoutInputs);
                   context.SaveChanges();
                }
             }
             catch (Exception ex)
             {
                throw ex;
             }
          }
          // return RedirectToAction("Index", new { id  = workout.ClientId});


          workoutViewModel.Clients = GetClients();
          workoutViewModel.Weeks = GetWeeks(client_id);
          workoutViewModel.Workouts = GetWorkouts(client_id);
          workoutViewModel.workout = workout;
          workoutViewModel.WorkoutInput = workoutInputs;

          return RedirectToAction("Index", "Workouts", new { id = client_id });
       }
       else
          return RedirectToAction("AccessDenied", "Error");
       }
       return RedirectToAction("AccessDenied", "Error");
    }

参数UploadedVideo和锻炼已填充,但尚未输入executionInput。

视图的代码:

<td>
@using (Html.BeginForm("UpdateClient", "Workouts", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
   <label>Weight Lifted: </label>
   @Html.TextBoxFor(m => m.WorkoutInput.Weight, new { @Value = workoutInput.Weight, @class = "form-control" })


   <label>Notes: </label>
   @Html.TextBoxFor(m => m.WorkoutInput.Notes, new { @Value = workoutInput.Notes, @class = "form-control" })
   @Html.TextBox("UploadedVideo", "", new { type = "file" })


   @Html.TextBoxFor(m => m.workout.workout_id, new { @Value = workoutId, @class = "hidden" })
   @Html.TextBoxFor(m => m.workout.ClientId, new { @Value = clientId, @class = "hidden" })

   <input type="submit" value="Update" />
}

视图的模型:

public class WorkoutViewModel
{
    public List<Workout> Workouts { get; set; }
    public Client Client { get; set; }
    public Workout workout { get; set; }
    public List<Week> Weeks { get; set; }
    public List<Client> Clients { get; set; }
    public List<Message> Messages { get; set; }
    public Program Program { get; set; }
    public Chat Chat { get; set; }
    public Message Message { get; set; }
    public List<WorkoutInputs> WorkoutInputs { get; set; }
    public WorkoutInputs WorkoutInput { get; set; }
}

PS:我已经尝试过隐藏字段。

2 个答案:

答案 0 :(得分:0)

使用WorkoutViewModel作为第三个参数,可以成功填充WorkoutViewModel的类的WorkoutInput的值。

答案 1 :(得分:-2)

除上传的文件外,我将尝试仅接受一个附加参数。 您可以创建另一个包含两个当前模型作为属性的模型。

编辑:详细说明:未经验证,我对ASP.NET的理解是,控制器通常将输入从一个源绑定到一个输入参数。 如果是控制器,则可以通过访问Request属性来访问各个字段。