如何在ASP.NET MVC和实体框架中进行预填充的表单

问题描述

因此,我在数据库中有一个客户端列表,其中包含有关它们的各种信息。我需要使用向下滑动的表格将所有这些信息打印出来,其中已经包含了它们的当前信息,并且能够添加或更改它。

我能够打印出所有客户端及其信息,但是当我尝试编辑表单时,即使没有设置禁用的属性,它也会被禁用。

这是控制者

public ActionResult Index()
    {
        var context = new U2XPlanningAutomationToolEntities();
        string query = "SELECT * FROM Clients";
        IEnumerable<Client> data = context.Database.sqlQuery<Client>(query);
        List<Client> clients = new List<Client>(data);

        ViewBag.Clients = clients;
        string email = Session["email"] as string;
        if (!String.IsNullOrEmpty(email))
        {
            return View();
        }

        return View("../Account/Login");
    }

视图

@foreach (var client in ViewBag.Clients)
{
    string plan_changes = client.plan_changes;
    string critical_milestones = client.critical_milestones;
    string pdd = client.pdd;
    string inbound = client.inbound;
    string outbound = client.outbound;
    string other = client.other;

    <div class="row client">
        <div class="col-sm-3"><span id="@client.id" class="glyphicon glyphicon-plus dropdownBtn"></span><p>@client.name</p></div>
        <div class="col-sm-3"><p>@client.oep_start</p></div>
        <div class="col-sm-3"><p>@client.oep_end</p></div>

        <div class="col-sm-3 text-right"><button id="@client.id" class="btn btn-primary delete-client">Delete</button></div>

        <div class="col-sm-12 slider" id="slider_@client.id">
            @using (Html.BeginForm("UpdateClient","Home",FormMethod.Post))
            {
                <div class="col-sm-4">
                    <input type="hidden" name="id" value="@client.id" />
                    @Html.LabelFor(c => c.plan_changes,"Plan Changes")
                    @*<textarea name="plan_changes" class="form-control" cols="20" rows="2">@plan_changes</textarea>*@
                    @Html.TextArea("plan_changes",plan_changes,new { @class = "form-control" })
                   
                    @Html.LabelFor(c => c.critical_milestones,"Critical Milestones")
                    @Html.TextArea("critical_milestones",critical_milestones,new { @class = "form-control" })
                </div>

                <div class="col-sm-4">
                    @Html.LabelFor(c => c.pdd,"Plan Document Design")
                    @Html.TextArea("pdd",pdd,new { @class = "form-control" })

                    @Html.LabelFor(c => c.inbound,"Inbound")
                    @Html.TextArea("inbound",inbound,new { @class = "form-control" })
                </div>

                <div class="col-sm-4">
                    @Html.LabelFor(c => c.outbound,"Outbound")
                    @Html.TextArea("outbound",outbound,new { @class = "form-control" })

                    @Html.LabelFor(c => c.other,"Other")
                    @Html.TextArea("other",other,new { @class = "form-control" })
                </div>
                <div class="col-sm-12 text-center">
                    <input type="submit" value="Update" name="update" class="btn btn-primary" />
                </div>
            }
            
        </div>
    </div>

    }

如果我的方法对您来说很奇怪,那可能是因为我是MVC ASP.NET的新手,我来自PHP背景。

谢谢!

解决方法

请在视图中使用IEnumerable<Client>而不是将其放入视图包中。您可以尝试在Html.BeginForm中传递ID吗?您的UpdateClient类中是否有HomeController方法?