微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何在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方法?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。