Reputation: 412
I am making my MVC application. My view PickGroupForHomework
is redirected to by
return RedirectToAction("PickGroupForHomework", "Account", new { subject_id = id, qty=model.qty });
Of course subject_id
and qty
are parameters of PickGroupForHomeworkViewModel
. The controller looks like this:
public ActionResult PickGroupForHomework(PickGroupForHomeworkViewModel model)
{
ClassDeclarationsDBEntities2 entities = new ClassDeclarationsDBEntities2();
model.groups = entities.Groups.ToList();
model.users = entities.Users.ToList();
int id = model.subject_id;
var subj = entities.Subjects
.Where(b => b.class_id == model.subject_id)
.FirstOrDefault();
if (subj != null)
{
model.subject_name = subj.name;
}
if (ModelState.IsValid)
{
DateTime myDate = DateTime.ParseExact(model.deadline+ " "+model.time, "yyyy-MM-dd HH:mm",
System.Globalization.CultureInfo.InvariantCulture);
ClassDeclarationsDBEntities2 entities2 = new ClassDeclarationsDBEntities2();
int total = entities2.Tasks.Count();
for (int i=0;i<model.task_names.Count;i++)
{
ClassDeclarationsDBEntities2 entities3 = new ClassDeclarationsDBEntities2();
int maxid;
if (total == 0)
{
maxid = 0;
}
else {
maxid = entities3.Tasks.Max(u => u.task_id);
}
var task = new Models.Task(model.task_names[i], model.subject_id, myDate, model.points[i], maxid + 1);
entities3.Tasks.Add(task);
entities3.SaveChangesAsync();
}
return RedirectToAction("OperationSuccess", "Account");
}
else
{
return View(model);
}
return View(model);
}
At first, everything loads correctly with correct URL including data passed from previous view. The form I am displaying now includes validation. If a user makes mistake in form, which indicades ModelState.IsValid=false
, the window is reloaded. But I do not know why it is reloaded without data passed from previous window: subject_id
and qty
. What am I doing wrong?
EDIT:
View: @model ClassDeclarationsThsesis.Models.PickGroupForHomeworkViewModel
@{
ViewBag.Title = "Pick Group For Homework"; }
<h2>Setting homework for @Model.subject_name</h2>
@foreach (var user in Model.users) {
if (user.email.Replace(" ", String.Empty) == HttpContext.Current.User.Identity.Name)
{
if (user.user_type.Replace(" ", String.Empty) == 2.ToString()|| user.user_type.Replace(" ", String.Empty) == 3.ToString())
{
using (Html.BeginForm("PickGroupForHomework", "Account", FormMethod.Post, new { @class = "form-horizontal", enctype = "multipart/form-data" })) {
@Html.AntiForgeryToken()
<hr />
<div class="form-group">
@Html.LabelFor(model => model.deadline, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.deadline, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.deadline, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.time, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.time, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.deadline, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.file, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
<div class="editor-field">
<input type="file" name="file" />
</div>
</div>
</div>
<div class="form-group">
<table>
<tr>
<th>
Name of task
</th>
<th>
Points
</th>
</tr>
@for (int i = 0; i < Model.qty; i++)
{
<tr>
<th>
<div class="form-group">
<div class="col-md-10">
@Html.TextBoxFor(m => m.task_names[i], new { @class = "form-control" })
</div>
</div>
</th>
<th>
<div class="form-group">
<div class="col-md-10">
@Html.TextBoxFor(m => m.points[i], new { @class = "form-control" })
</div>
</div>
</th>
</tr>
}
</table>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Submit" />
</div>
</div>
}
}
if (user.user_type.Replace(" ", String.Empty) == 1.ToString() )
{
<p>You do not have enough permissions to enter this page. Contact the administrator.</p>
}
}
}
Upvotes: 1
Views: 197
Reputation: 1438
Your problem is subject_id
is not in your form, so when you post back the form it sends 0
value to the server.
you need to add a field inside form , you can add a Text or hidden field
@Html.HiddenFor(m => m.subject_id)
Upvotes: 4