在我的模型类中,我有两个class
并且我在第三个class
中编写了前两个class
,在第一个类属性值中传递了控制器,但是其他两个类值不会在控制器字段中传递。
div class="page-content">
<div class="container-fluid">
<header class="section-header">
<div class="tbl">
<div class="tbl-row">
<div class="tbl-cell">
<h2>Company Registration Form</h2>
</div>
</div>
</div>
</header>
@using (Html.BeginForm(FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<section class="tabs-section">
<div class="tabs-section-nav tabs-section-nav-icons">
<div class="tbl">
<ul class="nav" role="tablist">
<li class="nav-item">
<a class="nav-link active" href="#tabs-1-tab-1" role="tab" data-toggle="tab">
<span class="nav-link-in">
<i class="font-icon font-icon-cogwheel"></i>
Company Registration Form
</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#tabs-1-tab-2" role="tab" data-toggle="tab">
<span class="nav-link-in">
<span class="glyphicon glyphicon-music"></span>
Company Social Network
</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#tabs-1-tab-3" role="tab" data-toggle="tab">
<span class="nav-link-in">
<i class="fa fa-product-hunt"></i>
Company Reference
</span>
</a>
</li>
</ul>
</div>
</div><!--.tabs-section-nav-->
<div class="tab-content">
<div role="tabpanel" class="tab-pane fade in active show" id="tabs-1-tab-1">
<br />
<br />
<section>
<div>
<div class="row">
<div class="col-lg-4">
<fieldset class="form-group">
@Html.LabelFor(model =>Model.company.CompanyName, new { @class = "form-label semibold control-label" })
@Html.TextBoxFor(model => model.company.CompanyName, new { @class = "form-control", placeholder = "Enter the Company Name" })
@Html.ValidationMessageFor(model => model.company.CompanyName)
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
@Html.LabelFor(model => model.company.ShortName, new { @class = "form-label semibold" })
@Html.TextBoxFor(model => model.company.ShortName, new { @class = "form-control", placeholder = "Enter the Short Name" })
@Html.ValidationMessageFor(model => model.company.ShortName)
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
@Html.LabelFor(model => model.company.Division, new { @class = "form-label semibold" })
@Html.TextBoxFor(model => model.company.Division, new { @class = "form-control", placeholder = "Enter the Division" })
@Html.ValidationMessageFor(model => model.company.Division)
</fieldset>
</div>
</div><!--.row-->
<div class="row">
<div class="col-lg-4">
<fieldset class="form-group">
@Html.LabelFor(model => model.company.Email, new { @class = "form-label semibold" })
@Html.TextBoxFor(model => model.company.Email, new { @class = "form-control", placeholder = "Enter your Email" })
@Html.ValidationMessageFor(model => model.company.Email)
</fieldset>
</div>
</div><!--.row-->
</div>
</section>
<input type="submit" name="Submit" id="Save" value="Save" class="btn btn-rounded btn-inline btn-success" onclick=" GetInfo();" />
</div><!--.tab-pane-->
<div role="tabpanel" class="tab-pane fade" id="tabs-1-tab-2">
<br />
<section>
<div>
<div class="row">
<div class="col-lg-4">
<fieldset class="form-group">
@Html.LabelFor(model => model.CompanySocial.FaceBookID, new { @class = "form-label semibold" })
@Html.TextBoxFor(model => model.CompanySocial.FaceBookID, new { @class = "form-control", @id = "txtFaceBookID", placeholder = "Enter the Facebook Link" })
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
@Html.LabelFor(model => model.CompanySocial.TwitterID, new { @class = "form-label semibold" })
@Html.TextBoxFor(model => model.CompanySocial.TwitterID, new { @class = "form-control", @id = "txtTwitterID", placeholder = "Enter the Twitter Link" })
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
@Html.LabelFor(model => model.CompanySocial.linkedinID, new { @class = "form-label semibold" })
@Html.TextBoxFor(model => model.CompanySocial.linkedinID, new { @class = "form-control", @id = "txtlinkedinID", placeholder = "Enter the Linkedin Link" })
</fieldset>
</div>
</div><!--.row-->
</div>
</section>
<input type="submit" name="Submit" value="Previous" class="btn btn-rounded btn-inline btn-primary prev-step" />
<input type="submit" name="Submit" id="saveSocial" value="Next" class="btn btn-rounded btn-inline btn-success" />
</div><!--.tab-pane-->
<div role="tabpanel" class="tab-pane fade" id="tabs-1-tab-3">
Tab 3
<br /><br />
<input type="submit" name="Submit" value="Previous" class="btn btn-rounded btn-inline btn-primary prev-step" />
<input type="submit" name="Submit" value="Finish" class="btn btn-rounded btn-inline btn-success" />
</div><!--.tab-pane-->
</div><!--.tab-content-->
</section><!--.tabs-section-->
}
</div>
</div>
Ajax Call方法:
<script>
function GetInfo() {
var company = { companyName: document.getElementById('CompanyName').value, shortName: document.getElementById('ShortName').value, division: document.getElementById('Division').value, Email: document.getElementById('Email').value }
$.ajax({
type: "POST",
url: "/Company/AddCompany",
data: '{cmp:' + JSON.stringify(company) + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
$(document).ready(function () {
$("#saveSocial").click(function (e) {
e.preventDefault();
GetInfo1();
});
});
function GetInfo1() {
//var Social = { faceBook: document.getElementById('FaceBookID').value, twitter: document.getElementById('TwitterID').value, linkedin: document.getElementById('linkedinID').value }
var Social = { "facebook": $("#txtFaceBookID").val() };
$.ajax({
type: "POST",
url: "/Company/AddSocial",
data: '{Social:' + JSON.stringify(Social) + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
</script>
单击按钮文本框时,在ActionResult AddSocial
控制器方法中价值未通过..
ActionResult AddCompany in this controller method the value is passed.
我已附上图片了:
班级方法:
[Table("CompanyRegistration")]
public class Company
{
[Key]
public int CompanyID { get; set; }
[Display(Name = "Company Name")]
[Required(ErrorMessage = "Enter the Company Name")]
public string CompanyName { get; set; }
[Display(Name = "Short Name")]
[Required(ErrorMessage = "Enter the Short Name")]
public string ShortName { get; set; }
public string Division { get; set; }
public string Email { get; set; }
}
[Table("CompanySocialNetwork")]
public class CompanySocial
{
[Key]
public int CompanySocialID { get; set; }
public int CompanyID { get; set; }
public string FaceBookID { get; set; }
public string TwitterID { get; set; }
public string linkedinID { get; set; }
}
public class MainModel
{
public Company company { get; set; }
public CompanySocial CompanySocial { get; set; }
}
答案 0 :(得分:1)
您只是没有将正确的模型传递给控制器。
function GetInfo1() {
//var Social = { faceBook: document.getElementById('FaceBookID').value, twitter: document.getElementById('TwitterID').value, linkedin: document.getElementById('linkedinID').value }
var Social = { "facebook": $("#txtFaceBookID").val() };
$.ajax({
type: "POST",
url: "/Company/AddSocial",
data: '{Social:' + JSON.stringify(Social) + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
将传递类似
的参数{
faceBook: "",
twitter: "",
linkedin: ""
}
但你需要
{
company: {},
CompanySocial: {
FaceBookID: "",
TwitterID: "",
linkedinID: ""
}
}
因此您必须将GetInfo1
更改为
function GetInfo1() {
var Social = { FaceBookID: document.getElementById('FaceBookID').value, TwitterID: document.getElementById('TwitterID').value, linkedinID: document.getElementById('linkedinID').value }
var mainModel = {};
mainModel.CompanySocial = Social;
$.ajax({
type: "POST",
url: "/Company/AddSocial",
data: JSON.stringify(mainModel),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}