通过从mvc razor中的下拉列表中选择id值,从db显示多个文本框值

时间:2015-03-18 14:29:33

标签: razor model-view-controller

这是我的下拉列表“Onchange”事件的脚本

    <script src="~/Scripts/jquery-1.7.1.js"> </script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#PId").change(function () {
                alert("Change Event"); 

                 @foreach (var item in Model)
                {                
                    var pid = item.PId.ToString();
                    var id = Request["PId"];

                    if(pid == id)
                    {                    
                        var email = item.PEmail;
                        var phn1 = item.Phn1;
                        var gender = item.Gender;
                    }
                }
       alert("123");                   

                alert($("#PEmail", "#Phn1", "#Gender", $(this)).show());
            });
        });    
    </script>
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>PatientMaster</legend>
        <table>
            <tr>       
                <td> @Html.Label("PName") </td>
                <td> @Html.DropDownList("PId", @ViewBag.PId as SelectList, "---Select---", new { onchange = "PId" }) </td>
            </tr>
            <tr>
                <td> @Html.Label("PAdd") </td>
                <td> @Html.TextBox("PAdd") </td>

                <td> @Html.Label("PCity") </td>
                <td> @Html.TextBox("PCity") </td>
            </tr>
            <tr>
                <td> @Html.Label("Gender") </td>   
                <td> @*<input id="gender" name="gender"/>*@
                    @Html.TextBox("Gender")
                </td>
            </tr>
            <tr>
                <td> @Html.Label("DOB") </td>
                <td> @Html.Editor("DOB") </td>

                <td> @Html.Label("PEmail") </td>
                <td> @*<input id="PEmail" type="text" />*@
                    @Html.TextBox("PEmail") </td>
            </tr>      
            <tr>
                <td> @Html.Label("Phn1") </td>
                <td> @*<input id="phn1" type="text" />*@
                    @Html.TextBox("Phn1") </td>

                <td> @Html.Label("Phn2") </td>
                <td> @Html.TextBox("Phn2") </td>
            </tr>
            <tr>
                <td> @Html.Label("PStatus") </td>
                <td> @Html.DropDownList("Status")
                </td>

                <td> @Html.Label("RefNo") </td>
                <td> @Html.TextBox("RefNo")</td>
            </tr>            
        <tr>
            <td> @Html.Label("Pswd") </td>
            <td> @Html.TextBox("Pswd") </td>
        </tr>
        </table>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

如何从数据库中显示文本框中的人员的电子邮件,phn和性别以更新值。我使用“ViewBag”来显示下拉列表。我应该在“@ Html.DisplayFor”的CSHTML代码中传递什么值 这是get方法的控制器代码:

//Get
        public ActionResult Update()
        {
            PatientMaster pm = new PatientMaster();
            List<PatientMaster> Patient = new List<PatientMaster>();

            using (GPediaEntities gp = new GPediaEntities())
            {
                Patient = gp.PatientMasters.OrderBy(a => a.PId).Distinct().ToList();
            }

            ViewBag.PId = new SelectList(Patient, "PId", "PName").Distinct().ToList();

            foreach (var item in Patient)
            {
                if (item.PId.ToString() == Request["PId"])
                {
                    string email = item.PEmail;
                    string gender = item.Gender;
                    string phn1 = item.Phn1;
                }                
            }

            List<SelectListItem> status = new List<SelectListItem>();
            status.Add(new SelectListItem { Text = "Existing Patient", Value = "Existing" });
            status.Add(new SelectListItem { Text = "New Patient", Value = "New" });
            ViewBag.Status = new SelectList(status, "Value", "Text", "Existing Patient");

            if (pm.PId.Equals(Request.Params["PId"]))
            {
                pm.PName = pm.PName;
                pm.Gender = pm.Gender;
                pm.PEmail = pm.PEmail;
                pm.Phn1 = pm.Phn1;
            }

            return Json(pm.PEmail);
        }

//邮

[HttpPost]
public ActionResult Update(PatientMaster pm)
{
    List<PatientMaster> Patient = new List<PatientMaster>();

    using (GPediaEntities gp = new GPediaEntities())
    {

            Patient = gp.PatientMasters.OrderBy(a => a.PId).Distinct().ToList();
        }

        ViewBag.PId = new SelectList(Patient, "PId", "PName").Distinct();
        pm.PName = pm.PId.ToString();


        if (ModelState.IsValid)
        {
            using (GPediaEntities gp = new GPediaEntities())
            {                    
                db.PatientMasters.Add(pm);
                db.SaveChanges();

                pm = null;
            }
        }

        return View(pm);
    }

提前完成

1 个答案:

答案 0 :(得分:0)

尝试Request.Params["paramName"]