0
votes

I am doing my MVC application. I have a view that gets data from another view. This view is a form to fill.

public ActionResult AddGroupsQty(int qty, int id)
        {

            var model = new AddGroupsQtyViewModel();
            model.subject_id = id;
            model.qty = qty;
            ClassDeclarationsDBEntities1 entities1=new ClassDeclarationsDBEntities1();
            var subj = entities1.Subjects
                    .Where(b => b.class_id == model.subject_id)
                    .FirstOrDefault();
            model.subject_name = subj.name;
            if (ModelState.IsValid)
            {
                int maxId = 0;
                int total = 0;

                total = entities1.Groups.Count();
                if (total == 0)
                {
                    maxId = 0;
                }
                else
                {
                    maxId = entities1.Groups.Max(u => u.group_id);

                }
                for (int i = 0; i < qty; i++)
                {
                    var teacher = entities1.Users
                    .Where(b => b.email.Replace(" ", String.Empty) == model.teacher_emails[i].Replace(" ", String.Empty))
                    .FirstOrDefault();
                    var group=new Models.Group(id, maxId+1, model.group_names[i], teacher.user_id);
                }
                return RedirectToAction("OperationSuccess", "Account");

            }
            return View(model);
        }

View model:

public class AddGroupsQtyViewModel
{
    public int qty { get; set; }
    public int subject_id { get; set; }
    public string subject_name { get; set; }
    [Required]
    [Display(Name = "Name of group")]
    public List<string> group_names { get; set; }
    [Required]
    [Display(Name = "Email of teacher")]
    public List<string> teacher_emails { get; set; }
}

And finally my view:

@using System.IdentityModel.Configuration
@using System.Web.UI.WebControls
@model ClassDeclarationsThsesis.Models.AddGroupsQtyViewModel

@{
    ViewBag.Title = "AddGroupsQty";
}

<h2>Add Groups to @Model.subject_name</h2>
@if (Model != null)
{


        using (Html.BeginForm("AddGroupsQty", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
        {
            @Html.AntiForgeryToken()
            <h4>Insert data</h4>
            <hr />
            <table>
                <tr>
                    <th>
                        @Html.ValidationSummary("", new { @class = "text-danger" })
                        <div class="form-group">
                            @Html.LabelFor(m => m.group_names, new { @class = "col-md-2 control-label" })
                        </div>
                    </th>
                    <th>
                        @Html.ValidationSummary("", new { @class = "text-danger" })
                        <div class="form-group">
                            @Html.LabelFor(m => m.teacher_emails, new { @class = "col-md-2 control-label" })
                        </div>
                    </th>
                </tr>
                @for (int i = 0; i < Model.qty; i++)
                {
                    <tr>
                        <th>
                            @Html.TextBoxFor(m => m.group_names[i], new { @class = "form-control" })
                        </th>
                        <th>
                            @Html.TextBoxFor(m => m.teacher_emails[i], new { @class = "form-control" })


                        </th>
                    </tr>
                }
            </table>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" class="btn btn-default" value="Submit" />
                </div>
            </div>
                    }
                        }

What more or less it does is it takes qty and generates a table for this many rows. Then, the rows have text boxes to fill in, which should represent data from my View model. However, when I submit, I get such error:

The parameters dictionary contains a null entry for parameter 'qty' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult AddGroupsQty(Int32, Int32)' in 'ClassDeclarationsThsesis.Controllers.AccountController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Nazwa parametru: parameters enter image description here

How do I go about this problem?

1
I don´t understood very well what you are trying to do but the error you are getting is because you have pointed to an action "AddGroupsQty" that requires two parameters to be sent: int qty, int id, and neither are being sent, so it can not route to that action throwing that exception. Regards. - dime2lo
But the window opens, because it gets required parameters. But then, in the view, when i click Submit (see the view) it crashes. @dime2lo - Maciej Miśkiewicz
Do you have another action with the same name as the one you posted? If don´t that one from the question has an http attribute (HttpGet or HttpPost) above it? - dime2lo
It has none @dime2lo - Maciej Miśkiewicz
I believe you trying to do too much with that action. As long as I understood the action is being used to render the initial state (what I believe it is workind) and it is already being used trying to modify the data already displayed (which is not working). But, as I said before, the exception you are getting is because the parameters are not being provided when the form is submitted. You you want to make a test (so the exception stops happening) you could add the parameters that are missing to the @Html.BeginForm or put them in the body of the form. - dime2lo

1 Answers

1
votes

You are not passing any value for qty after the form submission.

@Html.EditorFor(m => m.qty, new { @class = "form-control" })
@Html.HiddenFor(m=>m.id)

or

@using System.IdentityModel.Configuration
@using System.Web.UI.WebControls
@model ClassDeclarationsThsesis.Models.AddGroupsQtyViewModel

@{
ViewBag.Title = "AddGroupsQty";
}

<h2>Add Groups to @Model.subject_name</h2>
@if (Model != null)
{


    using (Html.BeginForm("AddGroupsQty", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
    {
   @Html.HiddenFor(m=>m.qty)    <%-- qty will be found my the controller after form submission --%>

   @Html.HiddenFor(m=>m.id)     <%-- id will also be found my the controller after form submission --%>
        @Html.AntiForgeryToken()
        <h4>Insert data</h4>
        <hr />
        <table>
            <tr>
                <th>
                    @Html.ValidationSummary("", new { @class = "text-danger" })
                    <div class="form-group">
                        @Html.LabelFor(m => m.group_names, new { @class = "col-md-2 control-label" })
                    </div>
                </th>
                <th>
                    @Html.ValidationSummary("", new { @class = "text-danger" })
                    <div class="form-group">
                        @Html.LabelFor(m => m.teacher_emails, new { @class = "col-md-2 control-label" })
                    </div>
                </th>
            </tr>
            @for (int i = 0; i < Model.qty; i++)
            {
                <tr>
                    <th>
                        @Html.TextBoxFor(m => m.group_names[i], new { @class = "form-control" })
                    </th>
                    <th>
                        @Html.TextBoxFor(m => m.teacher_emails[i], new { @class = "form-control" })


                    </th>
                </tr>
            }
        </table>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" class="btn btn-default" value="Submit" />
            </div>
        </div>
                }
                    }