Я пытаюсь установить все три объекта моей модели через BeginForm / TextboxFor и HTTP post. Первые два параметра формы работают, но по какой-то причине последний параметр не работает и не заполняется никакими значениями, которые я ввожу в текстовое поле.
Результат действия в моем контроллере:
[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 и Workout заполнены, но WorkoutInput - нет.
Код вида:
<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: я уже пробовал скрытые поля.