视图数据没有传递给控制器。请注意,我正在使用Entity Framework.试图设置Jobcard的值,以便我可以保存它们,但字段没有绑定到模型。请任何帮助将是感激的帮助我解决这个问题。
查看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)'" />
主页控制器
[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>();
//}
}
查看型号
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; }
}
}
添加完整的html代码
@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> <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 [email protected] 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>
}
```
尝试将表单值绑定到控制器中的参数上
public ActionResult SaveJobcard([FromForm]Jobcard jobcard)
@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" />
}
试试这个代码
解决这个问题的方法是,必须在你要提交的html里面添加表单Html标签来包装。
错了
@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>
}
完美工作
@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>
}