0

Here is Model

public class candidate_votes
{

    public int ff_id_fk { get; set; }
    public int cmember_id { get; set; }
    public int cparty_id { get; set; }
    public int cand_votos { get; set; }
}

Here is View that i am showing data to insert this data into db so that data is in multi values means bulkdata i want every row add in db untill count

        foreach (var doc in Model)
        {
            <div class="row justify-content-center">
                <div class="col-sm-2">
                    <label>Candidate Name</label>
                    <p>@doc.member_name</p>
                    <input type="hidden" name="cmember_id[]" value="@doc.member_id" class="form-control" />
                </div>
                <div class="col-sm-2">
                    <label>Party Name</label>
                    <p>@doc.party_name</p>
                    <input type="hidden" name="cparty_id[]" value="@doc.party_id_fk" class="form-control" />
                </div>
                <div class="col-sm-2">
                    <div class="form-group">
                        <label>Total Votes</label>
                        <input type="text" name="cand_votos[]" class="form-control" />
                    </div>
                </div>
            </div>
        }

Here is controller which i am using to post data and on same time first i get data on view with other controller than i am posting that data to this controller

    public ActionResult ps_formForty(candidate_votes cand )
    {
                    Dictionary<string, string> data2 = new Dictionary<string, string>();
        for (int i = 0; i < cand.cmember_id.count; i++)
        {
            data2.Add("cmember_id", (cand.cmember_id).ToString());
            data2.Add("cparty_id", cand.cparty_id.ToString());
            data2.Add("cand_votos", cand.cand_votos.ToString());
            DbObject.Insert("candidate_votes", data2);

        }


        return View();
    }

i want something like this and but i couuld not apply loop on candidate_votes cand object Thanks in Advance

Asif Shakir
  • 95
  • 1
  • 12
  • Suggest you read [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) to understand how to generate form controls for a collection (but then the parameter in your POST method is not even a collection so it not clear what your trying to do) –  Aug 15 '18 at 08:27
  • The `foreach` loop creates multiple hidden `input` elements with same `name` attribute, which is invalid HTML. You need to use `HiddenFor` and viewmodel indexing to get the model bound into POST method. – Tetsuya Yamamoto Aug 15 '18 at 08:28

2 Answers2

0

As @Stephen says. You can only loop a collection and not a simple class. You need to return an array from the UI. The C# goes something like this:

    public ActionResult ps_formForty(List<candidate_votes> cand )
    {
        Dictionary<string, string> data2 = new Dictionary<string, string>();
        for (int i = 0; i < cand.count; i++)
        {
            data2.Add("cmember_id", (cand.cmember_id).ToString());
            data2.Add("cparty_id", cand.cparty_id.ToString());
            data2.Add("cand_votos", cand.cand_votos.ToString());
            DbObject.Insert("candidate_votes", data2);

        }


        return View();
}

From the JavaScript, you may pass the array of items.

$.ajax({
   type: "POST",
   data: array,
   url: url,
   success: function(msg){
     //Your code
   }
});

On the other hand, if you do not have an array, and only one object of candidate_votes, then you should not use a loop. Because there is only one object there.

    public ActionResult ps_formForty(candidate_votes cand )
    {
        Dictionary<string, string> data2 = new Dictionary<string, string>();
            data2.Add("cmember_id", (cand.cmember_id).ToString());
            data2.Add("cparty_id", cand.cparty_id.ToString());
            data2.Add("cand_votos", cand.cand_votos.ToString());
            DbObject.Insert("candidate_votes", data2);

        return View();
}

If what you want is the number of letters in the cmember_id, then use cmember_id.Length instead of cmember_id.count.

sandeepani
  • 353
  • 3
  • 12
  • but the problem is that i want like this ..... the code is same as it is but i cannot apply loop that,s my question how i can apply loop like this and i want like this code i have written ... i think you understand what i am saying ! – Asif Shakir Aug 15 '18 at 09:07
  • Okay.. Only now noticed, candidate_votes is not a collection as @stephen_muecke says. You can only loop a collection and not a simple class. You need to return an array from the UI. – sandeepani Aug 15 '18 at 09:52
  • So how i can post an array to controller ?? – Asif Shakir Aug 15 '18 at 17:56
  • I have edited the earlier answer to reflect the changes. Do check it out. – sandeepani Aug 16 '18 at 04:15
0

Just placed index in view for array of object Then get list of object from your action method.

@Model System.Generic.Collection.List

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@*Note : Replace Your Controller Name*@
@using (Html.BeginForm("ps_formForty", "Home", FormMethod.Post))
{
    var i = 0;
    foreach (var doc in Model)
    {
        <div class="row justify-content-center">
            <div class="col-sm-2">
                <label>Candidate Name</label>
                <p>@doc.member_name</p>
                <input type="hidden" name="list[@i].cmember_id" value="@doc.cmember_id" class="form-control" />
            </div>
            <div class="col-sm-2">
                <label>Party Name</label>
                <p>@doc.party_name</p>
                <input type="hidden" name="list[@i].cparty_id" value="@doc.cparty_id" class="form-control" />
            </div>
            <div class="col-sm-2">
                <div class="form-group">
                    <label>Total Votes</label>
                    <input type="text" name="cand_votos[@i]" class="form-control" />
                </div>
            </div>
        </div>
        i++;
    }
    <input type="submit">
}



    [HttpPost]
    public ActionResult ps_formForty(List<candidate_votes> cand)
    {
        Dictionary<string, string> data2 = new Dictionary<string, string>();
        foreach (var item in cand)
        {
            data2.Add("cmember_id", (item.cmember_id).ToString());
            data2.Add("cparty_id", item.cparty_id.ToString());
            data2.Add("cand_votos", item.cand_votos.ToString());
            DbObject.Insert("candidate_votes", data2);

        }
        return View();
    }
Abiuth Arun
  • 169
  • 1
  • 4
  • **@Model System.Generic.Collection.List** i have add this model to view but when i submit form error **Object Cand Reference is Null ** foreach (var item in **cand**) { } – Asif Shakir Aug 15 '18 at 18:05
  • 1
    Please compare below suggestion with your code 1)@Model System.Generic.Collection.List< **yourNameSpace.candidate_votes** > 2) `` – Abiuth Arun Aug 16 '18 at 04:17