我是新的asp .net mvc 4。
我有一个表格,名称,名称,数量和手机号码,一个提交按钮和一个按钮。
首先我必须填写表格然后当我点击提交按钮然后它将存储在数据库中,当我点击预览按钮时,我将在所有字段中插入的数据,它将显示在另一个表中。
Facilities.cshtml
@using (Html.BeginForm("Facilities", "OfficialInformation", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table class="grid" border="1" cellspacing="0" cellpadding="0">
<tr class="gridAlternateRow">
<td>
<span style="color:red;">*</span> Name
</td>
<td colspan="4">
<div class="row">
@*@Html.TextBoxFor(m=>m.txtfirstName)*@
@*@Html.DisplayFor(m => m.txtfirstName)*@
@Html.TextBox("empname", (string)ViewBag.empname, new { @readonly = "readonly" })
</div>
</td>
</tr>
<tr>
<td>
Role Designation As Per HR Record
</td>
<td colspan="4">
<div class="row">
@*@Html.TextBoxFor(m=>m.txtDesignation)*@
@Html.TextBox("empDesignation", (string)ViewBag.empDesignation, new {@readonly="readonly" })
</div>
</td>
</tr>
<tr>
<td>
<span style="color:#FF0000;">*</span>Mobile
</td>
<td>
<div class="row">
@Html.TextBoxFor(m=>m.intMobile)
<span style="color:red"> @Html.ValidationMessageFor(m => m.intMobile)</span>
</div>
</td>
</tr>
<tr class="gridAlternateRow">
<td><span style="color:#FF0000;">*</span>Quantity</td>
<td colspan="3">
<div class="row">
@Html.TextBoxFor(m => m.intQuantity)
<span style="color:red">@Html.ValidationMessageFor(m => m.intQuantity)</span>
</div>
</td>
</tr>
<input type="submit" value="Save" class="btnPrimary">
<input type="reset" value="Reset" class="btnPrimary">
<div id="previewtop">
<input type="button" value="Preview" class="btnPrimary" id="previewtop1"/>
</div>
<div id="previewdisplay">
<table class="grid">
<tr>
<td>
Name:@Html.DisplayFor(m=>m.txtFirstName)
</td>
</tr>
<tr>
<td>
@Html.DisplayFor(m=>m.txtDesignation)
</td>
</tr>
<tr>
<td>
@Html.DisplayFor(m=>m.intQuantity)
</td>
</tr>
<tr>
<td>
Mobile:@Html.DisplayFor(m=>m.intMobile)
</td>
</tr>
</table>
</div>
}
Controller.cs
[HttpGet]
public ActionResult Facilities()
{
int EmpId = 10002;
ViewBag.LocationList = new SelectList(db.mstLocationDetails, "intLocationId", "txtLocationName");
ViewBag.SubLocationlist = new SelectList(db.mstSubLocations, "intSubLocationId", "txtSubLocationName");
ViewBag.Deaprtmentlist = new SelectList(db.mstDepartmentLists, "intDeptartmentId", "txtDepartmentName");
ViewBag.BoardNumber = new SelectList(db.mstBoardNumberDetails, "intBoardNumberId", "intBoardNumber");
ViewBag.FaxNumber = new SelectList(db.mstBoardNumberDetails, "intBoardNumberId", "intFaxNumber");
ViewBag.Officeaddress = new SelectList(db.mstBoardNumberDetails,"intBoardNumberId","txtOfficeAddress");
mstEmpDetail emp = new mstEmpDetail();
ViewBag.managerid = new SelectList(db.mstManagers, "txtEmpMagerEmail", "txtEmpMagerEmail");
var emp2 = db.mstEmpDetails.Count(x => x.intEmpId == EmpId);
if (emp2 > 0)
{
emp = db.mstEmpDetails.Single(x => x.intEmpId == EmpId);
ViewBag.empname = emp.txtEmpFirstName;
}
var empdg = db.mstDesignationDetails.Single(x => x.intEmpId == EmpId);
ViewBag.empDesignation = empdg.txtDesignationName;
return View();
}
[HttpPost]
public ActionResult Facilities(TrnBusinessCardDetail1s bc)
{
db.TrnBusinessCardDetail1s.Add(bc);
db.SaveChanges();
Session["sg"] = "Your Request has been sent Successfully.";
return RedirectToAction("Facilities");
}
的jQuery
@Scripts.Render("~/bundles/jquery")
<script>
$(document).ready(function () {
$('#previewtop').click (function() {
$('#previewdisplay').show();
});
});
CSS
<style>
#previewdisplay
{
display:none;
}
</style>
Can any one tell me how to retrieve the value of the text box whatever I inserted that should
display in the preview button table while clicking on preview button.
在我点击预览按钮后运行应用程序后,它无法从同一表单中检索该值?
答案 0 :(得分:1)
您需要使用jquery从文本框中获取值,例如此
@Html.TextBoxFor(m=>m.txtfirstName)
可以使用id txtfirstName访问此文本框值,如下所示:
$('#txtfirstName').val();
然后,您需要使用jquery .html()方法将此值附加到标签上 将您的jquery更改为此
$('#previewtop').click (function(){
$('#previewdisplay').show();
var fname= $('#txtfirstName').val();
$('.myInput').html(fname);
});
并在cshtml中添加span并使用labelfor而不是displayfor
@Html.LabelFor(model => model.txtfirstName)
<span class="myInput"></span>
这是jsfiddle jsfiddle.net/mopa7xq1/