简体   繁体   中英

ASP.NET MVC C# : passing view data to controller

View data not passing to controller. Please note I am using Entity Framework. Trying to set the values of the Jobcard so that I can save them, but the fields are not binding to the model. Please any help would be appreciated in helping me solve this issue.

View Cshtml

@model Amax.Models.Jobcard

@using (Html.BeginForm("SaveJobcard", "Home", FormMethod.Post))
{
    @Html.EditorFor(model => model.Email)
}

<input class="btn btn-sm btn-outline-success type="submit" id="submit" value="Create Jobcard" onclick="location.href='@Url.Action("SaveJobcard", "Home" , FormMethod.Post)'" />

HomeController

[System.Web.Http.HttpPost]
public ActionResult SaveJobcard(Jobcard Jobcard)
{
    if (ModelState.IsValid)
    {
        try
        {
        }
        catch (Exception ex)
        {
        }
    }

    return View();
}

DBModel

namespace Amax.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Jobcard
    {
        public long JCId { get; set; }
        public Nullable<long> ClientId { get; set; }
        public Nullable<long> ContactId { get; set; }
        public string ContactNo { get; set; }
        public string Email { get; set; }
        public string VatNo { get; set; }
        public Nullable<bool> SLA { get; set; }
        public string Address { get; set; }
        public Nullable<System.DateTime> TofOrder { get; set; }
        public Nullable<System.DateTime> TofCompletion { get; set; }
        public Nullable<long> ClientSigId { get; set; }
        public string CSignature { get; set; }
        public Nullable<System.DateTime> ClientSigDate { get; set; }
        public Nullable<long> USigId { get; set; }
        public string USignature { get; set; }
        public string USigDate { get; set; }
        public Nullable<int> StatusId { get; set; }

        public List<JobCardItem> Item { get; set; }
    }
    //public JobCardItem()
    //{
    //    Item = new List<JobCardItem>();
    //}
}

View Model

namespace Amax.Models
{
    public class ViewModel
    {
        public Jobcard Jobcard { get; set; }
        public Client Client { get; set; }
        public Contact Contact { get; set; }
        public JobCardItem JobCardItem { get; set; }
        public Status Status { get; set; }
        public User Users { get; set; }
    }
}

Jobcard Object empty

Fields Populated on form Form Submit Network tab details

Adding in the full view html code

 @using (Html.BeginForm("SaveJobcard", "Home", FormMethod.Post))
        {
        <div class="col-md-12 col-sm-12" id="JobCard">
            <div class="row">
                <div class="col-md-6 col-sm-6">
                    <div class="row">
                        <div class="col-md-6 col-sm-6">
                            <label class="label" style="color:black;">Company Name :</label>
                        </div>
                        <div class="col-md-6 col-sm-6">
                            <select class="form-control" id="ClientId" value="ClientId">
                                @{
                                    <option value="0">
                                        Please Select
                                    </option>
                                    var Clients = ((Amax.Controllers.HomeController)this.ViewContext.Controller).GetClients();
                                    foreach (var client in Clients)
                                    {
                                        <option onclick="@Url.Action("GetContacts", "Home", new RouteValueDictionary(new { id = @client.ClientId }));" value="@client.ClientId">
                                            @client.ClientName
                                        </option>
                                    }

                                }
                            </select>
                        </div>
                    </div>
                    <br />
                    <div class="row">
                        <div class="col-md-6 col-sm-6">
                            <label class="label" style="color:black;">Contact No :</label>
                        </div>
                        <div class="col-md-6 col-sm-6">
                            <input type="text" class="form-control" id="ContactNo" name="ContactNo" />
                        </div>
                    </div>
                    <br />
                    <div class="row">
                        <div class="col-md-6 col-sm-6">
                            <label class="label" style="color:black;">Vat No :</label>
                        </div>
                        <div class="col-md-6 col-sm-6">
                            <input type="text" class="form-control" id="VatNo" name="VatNo" />
                        </div>
                    </div>
                </div>
                <div class="col-md-6 col-sm-6">
                    <div class="row">
                        <div class="col-md-6 col-sm-6">
                            <label class="label" style="color:black;">Contact Person :</label>
                        </div>
                        <div class="col-md-6 col-sm-6">
                            <select class="form-control" id="ContactId" name="ContactId">
                                <option value="0">
                                    Please Select
                                </option>
                            </select>
                        </div>
                    </div>
                    <br />
                    <div class="row">
                        <div class="col-md-6 col-sm-6">
                            <label class="label" style="color:black;">Email</label>
                        </div>
                        <div class="col-md-6 col-sm-6">
                            @Html.EditorFor(model => model.Email)
                        </div>
                    </div>
                    <br />
                    <div class="row">
                        <div class="col-md-6 col-sm-6">
                            <label class="label" style="color:black;">SLA :</label>
                        </div>
                        <div class="col-md-3 col-sm-3">
                            <input type="checkbox" class="form-control" />
                        </div>
                        <div class="col-md-3 col-sm-3">
                            <input type="checkbox" class="form-control" />
                        </div>
                    </div>
                </div>
            </div>
            <br />
            <div class="row">
                <div class="col-md-12 col-sm-12">
                    <div class="row">
                        <div class="col-md-3 col-sm-3">
                            <label class="label" style="color:black;">Address :</label>
                        </div>
                        <div class="col-md-9 col-sm-9">
                            <input type="email" class="form-control" id="Address" name="Address" />
                        </div>
                    </div>
                </div>
            </div>
            <br />
            <div class="row">
                <div class="col-md-12 col-sm-12">
                    <div class="row">
                        <div class="col-md-6 col-sm-6">
                            <div class="row">
                                <div class="col-md-6 col-sm-6"> <label class="label" style="color:black;">Time & Date of Order :</label></div>
                                <div class="col-md-6 col-sm-6"><input type="date" /> </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="row">
                                <div class="col-md-6 col-sm-6"> <label class="label" style="color:black;">Time & Date of Completion :</label></div>
                                <div class="col-md-6 col-sm-6"><input type="date" /></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <br />
            <div class="row">
                <div class="col-md-6 col-sm-6">
                    <div class="row">
                        <div class="col-md-6 col-sm-6">
                            <label class="label" style="color:black;">Assigned Person :</label>
                        </div>
                        <div class="col-md-6 col-sm-6">
                            <select class="form-control" id="assigned" name="assigned">
                                @{
                                    <option value="0">
                                        Please Select
                                    </option>
                                    var Users = ((Amax.Controllers.HomeController)this.ViewContext.Controller).GetUsers();
                                    foreach (var user in Users)
                                    {
                                        <option value="@user.UserId">
                                            @user.UserName @user.Surname
                                        </option>
                                    }

                                }

                            </select>
                        </div>
                    </div>
                </div>
                <div class="col-md-6 col-sm-6">
                    <div class="row">
                        <div class="col-md-6 col-sm-6">
                            <label class="label" style="color:black;">Status :</label>
                        </div>
                        <div class="col-md-6 col-sm-6">
                            <select class="form-control" id="StatusId" name="StatusId">
                                @{
                                    <option value="0">
                                        Please Select
                                    </option>
                                    var Status = ((Amax.Controllers.HomeController)this.ViewContext.Controller).GetStatus();
                                    foreach (var status in Status)
                                    {
                                        <option value="@status.Id">
                                            @status.Status1
                                        </option>
                                    }

                                }

                            </select>
                        </div>
                    </div>
                </div>
            </div>
            <input class="btn btn-sm btn-outline-success type="submit" id="submit" value="Create Jobcard" onclick="location.href='@Url.Action("SaveJobcard", "Home" , FormMethod.Post)'" />
            <br />
            <br />
            <br />
            <div class="row">
                <div class="col-md-12 col-sm-12">
                    <div class="row">
                        <div class="col-md-6 col-sm-6">
                            <div class="row">
                                <div class="col-md-4"> <label class="label" style="color:black;">#</label></div>
                                <div class="col-md-8"> <label class="label" style="color:black;">Description</label></div>
                            </div>
                        </div>
                        <div class="col-md-6 col-sm-6">
                            <div class="row">
                                <div class="col-md-6"> <label class="label" style="color:black;">Unit Price</label></div>
                                <div class="col-md-6"> <label class="label" style="color:black;">Total</label></div>
                            </div>
                        </div>
                    </div>
                    <div class="row" id="JobItems">
                        @Html.EditorFor(model => model.Item)
                    </div>
                    <div class="col-md-2 col-sm-2"> <button class="btn btn-sm btn-success" id="btnAdd"> + </button>&nbsp;<button class="btn btn-sm btn-danger" id="remove_1"> - </button></div>
                    <br />
                    <div class="row">
                        <div class="col-md-6 col-sm-6">
                            <div class="row">
                                <div class="col-md-4">

                                </div>
                                <div class="col-md-8"></div>
                            </div>
                        </div>
                        <div class="col-md-6 col-sm-6">
                            <div class="row">
                                <div class="col-md-6 col-sm-6">Total</div>
                                <div class="col-md-6 col-sm-6"><label id="Jc_total">R 0.00</label></div>
                            </div>
                        </div>
                    </div>
                    <br />
                    <div class="row">
                        <div class="col-md-12 col-sm-12">
                            Terms:<br />
                            1. All equipment remains the property of AMAX until fully paid and client acknowledges that AMAX reserves the right to remove the equipment in the absence of payment.<br />
                            Client acknowledges that the equipment belongs to AMAX until fully paid and that the client shall not, under any circumstances, alienate, tarnish, destroy or erode the equipment.<br />
                            Client shall also ensure reasonable use of the equipment and shall ensure that the equipment is not exposed to negative elements which could alienate, tarnish, destroy or erode the goods.<br />
                            2. Customer agrees that AMAX may remove equipment if not paid within 7 working days of job completion.<br />
                            3. Customer will allow AMAX access to installation site to remove equipment that is unpaid.<br />
                            4. All Equipment carries a 1 year manufacturer’s guarantee.<br />
                            5. The Customer Agrees that the job has been done to his or her satisfaction
                        </div>
                    </div>
                    <br />
                    <div class="row">
                        <div class="col-md-12 col-sm-12">

                            <div class="row">
                                <dic class="col-md-12 col-sm-12">
                                    Banking Details: Bank: Acc Name: Acc No: Branch Code: Type:
                                    FNB Amax Electronics 623402 97028 251045 Cheque
                                    Proof of payment: Mail accounts@amaxelectronics.co.za Fax: 012 543 2394
                                    Ref: JN Number
                                </dic>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
                <div class="row">
                    <div class="col-md-12 col-sm-12 pull-right">

                        @using (Html.BeginForm("PDF", "Home", FormMethod.Post))
                        {
                            <input type="hidden" name="Styles" id="Styles" />
                            <input type="hidden" name="ExportData" id="ExportData" />
                            <input type="submit" id="btnSubmit" value="Generate PDF" />
                        }
                        @*<button class="btn btn-sm btn-outline-primary" onclick="@Url.Action("PDF", "Home")">Generate PDF</button>*@
                    </div>
                </div>

            }
    ```

Try binding the form values to the parameter in your controller

public ActionResult SaveJobcard([FromForm]Jobcard jobcard)

the solution to the problem was that one has to add form Html tags to wrap inside the html you want to submit

Wrong

 @using (Html.BeginForm("SaveJobcard", "Home", FormMethod.Post))
        {
        <div class="col-md-12 col-sm-12" id="JobCard">
            <div class="row">
                <div class="col-md-6 col-sm-6">
                    <div class="row">
                        <div class="col-md-6 col-sm-6">
                            <label class="label" style="color:black;">Company Name :</label>
                        </div>
                        <div class="col-md-6 col-sm-6">
                            <select class="form-control" id="ClientId" value="ClientId">
                                @{
                                    <option value="0">
                                        Please Select
                                    </option>
                                    var Clients = ((Amax.Controllers.HomeController)this.ViewContext.Controller).GetClients();
                                    foreach (var client in Clients)
                                    {
                                        <option onclick="@Url.Action("GetContacts", "Home", new RouteValueDictionary(new { id = @client.ClientId }));" value="@client.ClientId">
                                            @client.ClientName
                                        </option>
                                    }

                                }
                            </select>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    }

Works Perfect

 @using (Html.BeginForm("SaveJobcard", "Home", FormMethod.Post))
        {
        <form>
        <div class="col-md-12 col-sm-12" id="JobCard">
            <div class="row">
                <div class="col-md-6 col-sm-6">
                    <div class="row">
                        <div class="col-md-6 col-sm-6">
                            <label class="label" style="color:black;">Company Name:</label>
                        </div>
                        <div class="col-md-6 col-sm-6">
                            <select class="form-control" id="ClientId" value="ClientId">
                                @{
                                    <option value="0">
                                        Please Select
                                    </option>
                                    var Clients = ((Amax.Controllers.HomeController)this.ViewContext.Controller).GetClients();
                                    foreach (var client in Clients)
                                    {
                                        <option onclick="@Url.Action("GetContacts", "Home", new RouteValueDictionary(new { id = @client.ClientId }));" value="@client.ClientId">
                                            @client.ClientName
                                        </option>
                                    }

                                }
                            </select>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    }


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM