文本框在MVC Razor动态表中不显示值?

时间:2016-05-04 10:53:46

标签: javascript asp.net-mvc entity-framework razor

我是MVC的新手,并尝试为Course-Class结构实现动态表。 CourseCode将是来自前一个表单的值,动态表将包含与该特定CourseCode相关的所有类。 我的问题是,我必须在一个列中强制使用CourseCode(因为我的模型是一个包含所有四个字段的列表类型模型)。但是,我无法做到 在初始行之后的后续CourseCode文本字段中显示viewbag的值。这是由于这一行" $ trNew.find("输入")。val("");"我相信,但是我需要新行来为ClassCode,CLassName和Promotion字段留空,但CourseCode应该是一个默认包含viewbag CourseCode值的文本框。虽然在调试脚本时,CourseCode新添加的空白文本框的值设置为viewbag值,但文本框在我的视图中为空。我希望文本框在添加行时默认显示CourseCode,其他三个文本框应为空。

这是我的模特:

public partial class CourseClass
    {          

        [Key]
        [Column(Order = 0)]
        public string CourseCode { get; set; }
        [Key]
        [Column(Order = 1)]
        public string ClassCode { get; set; }
        public string ClassName { get; set; }
        public string Promotion{ get; set; }

        public virtual Course course{ get; set; }
        public virtual ICollection<Subject> subjects{ get; set; }
    }

这是我的控制器:

        [HttpGet]
        public ActionResult BIndexed(string CourseCode)
        {
            // This is only for show by default one row for insert data to the database
            ViewBag.CourseCode= CourseCode;
            List<CourseClass> ci = new List<CourseClass> { new CourseClass{ CourseCode= CourseCode, ClassCode= "", ClassName= "", Promotion= "" } };
            return View(ci);     
        }



    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult BIndexed(List<CourseClass> ci)
    {
        if (ModelState.IsValid)
        {                
                foreach (var i in ci)
                {
                    db.Class.Add(i);
                }
                db.SaveChanges();                   
                ModelState.Clear();
                return RedirectToAction("Index");                   
        }
        return View(ci);
    }

这是我的观点:

<table id="dataTable" border="0" cellpadding="0" cellspacing="0">

            <tr>
                <th style="display:none;>Course Code</th>
                <th>Class Code</th>
                <th>Class Name</th>
                <th>Promotion</th>
                <th></th>

            </tr>

            @if (Model != null && Model.Count > 0)
            {
                int j = 0;
                foreach (var i in Model)
                {
                    <tr style="border:1px solid black">


                        <td>@Html.TextBoxFor(a => a[j].CourseCode,(string)@ViewBag.CourseCode)</td>
                        <td>@Html.TextBoxFor(a => a[j].ClassCode)</td>
                        <td>@Html.TextBoxFor(a => a[j].ClassName)</td>
                        <td>@Html.TextBoxFor(a => a[j].Promotion)</td>
                        <td>
                            @if (j > 0)
                            {
                                <a href="#" class="remove">Remove</a>
                            }
                        </td>
                    </tr> 
                    j++;                        
                }
            }

    </table>

这是剧本:

$(document).ready(function () {

        //1. Add new row
        $("#addNew").click(function (e) {
            e.preventDefault();
            var $tableBody = $("#dataTable");
            var $trLast = $tableBody.find("tr:last");
            var $trNew = $trLast.clone();
        var suffix = $trNew.find(':input:first').attr('name').match(/\d+/);
        $trNew.find("td:last").html('<a href="#" class="remove">Remove</a>');

        $.each($trNew.find(':input'), function (i, val) {
            // Replaced Name
            var oldN = $(this).attr('name');
            var newN = oldN.replace('[' + suffix + ']', '[' + (parseInt(suffix) + 1) + ']');
            $(this).attr('name', newN);

            var j = parseInt(suffix) + 1;

            //Replaced value
            $trNew.find("input").val("");
            $("#U_CCODE").attr('value','@ViewBag.U_CCode');
           }

            // If you have another Type then replace with default value
            $(this).removeClass("input-validation-error");

          }

        });
        $trLast.after($trNew);

        // Re-assign Validation
        var form = $("form")
            .removeData("validator")
            .removeData("unobtrusiveValidation");
        $.validator.unobtrusive.parse(form);
    });

    // 2. Remove
    $(document).on("click", "a.remove", function (e) {
        e.preventDefault();
        $(this).parent().parent().remove();
    });

});

对此有任何帮助将不胜感激。谢谢!

0 个答案:

没有答案