使用C#MVC中的View创建/编辑实体

时间:2015-03-11 13:09:19

标签: c# asp.net-mvc twitter-bootstrap

我有一个View,我在其中使用@Html.EditorForModel()来创建和编辑SQL表中的信息。有了这个,我的功能有限,所以我想我会使用某种引导来使TextBoxes的布局更整洁。

我删除了(评论过)EditotForModel,添加了我自己的样式文本框。它看起来很棒,但Save并不成功。它会抛出错误

  

已经有一个与此命令关联的打开DataReader,必须先关闭它。

这是我的View(Book.cshtml),

@model PEF.IT.BoilerTest.Domain.CustomerModel
@{
    ViewBag.Title = "Add";
}
<h3><span style="text-decoration: underline;">Customer Information</span></h3>
<br />
@using (Html.BeginForm("SaveCustomer", "Companies"))
{
    @*@Html.EditorForModel()*@
    <input type="text" class="form-control" placeholder="Customer Name" aria-describedby="basic-addon1" id="CustomerName"><br />

    <input type="text" class="form-control" placeholder="Address Line 1" aria-describedby="basic-addon1" id="AddressLine1"><br />

    <input type="text" class="form-control" placeholder="Postcode" aria-describedby="basic-addon1" id="Postcode"><br />

    <input type="text" class="form-control" placeholder="Phone Number" aria-describedby="basic-addon1" id="PhoneNo"><br />

    <input type="text" class="form-control" placeholder="Boiler Name" aria-describedby="basic-addon1" id="BoilerName"><br />

    <input type="text" class="form-control" placeholder="Boiler Age" aria-describedby="basic-addon1" id="BoilerAge"><br/>

    <div class="input-group">
        <span class="input-group-addon">£</span>
        <input type="text" class="form-control" placeholder="72.55" aria-describedby="basic-addon1" id="ServiceRate">
    </div><br />

    <input type="text" class="form-control" placeholder="17/03/2015" aria-describedby="basic-addon1" id="ServiceDate"><br/>

    <button type="Submit" class="btn btn-primary">Save</button>

    @Html.ActionLink("Cancel", "Index", null, new { @class = "btn btn-warning" })
}

这是我的控制器,

    //Customers
    public ActionResult Book(long id)
    {
        return View(new CustomerModel
        {
            CompanyId = id
        });
    }

    [HttpPost]
    public ActionResult SaveCustomer(CustomerModel model)
    {
        _companyService.SaveCustomer(model);

        return RedirectToAction("Index");
    }

这是服务中的保存方法,

//Save a Customer
public void SaveCustomer(CustomerModel customer)
{
    using (var db = new BoilerServicingDbContext())
    {
        Customer entity;
        if (customer.CustomerId > 0)
        {
            entity = db.Customers.First(x => x.Id == customer.CustomerId);
        }
        else
        {
            entity = new Customer();
            db.Customers.Add(entity);

            //Fails here. If I used Editor For Model, no problem.
            var p = Map(db.Companies.AsNoTracking()).First(x => x.CompanyId == customer.CompanyId);

            var fromAddress = new MailAddress("someone@gmail.com", "Name");
            var toAddress = new MailAddress("someone2@gmail.com", "Name2");//new MailAddress(p.Email, p.CompanyName);
            const string fromPassword = "XXXX";
            const string subject = "New Email";

            string body = "<HTML>" +
                                "<BODY>" +
                                    "<P>Dear " + p.CompanyName + ",</P><BR/><BR/>" +
                                    "<P>Hope you are well.<BR/><BR/>" +
                                    "Kind Regards<BR/><BR/>" +
                                    "Name2" +
                                "</BODY>" +
                          "</HTML>";

            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
            };
            using (var message = new MailMessage(fromAddress, toAddress)
            {
                Bcc = { "newone@somewhere.com" },
                Subject = subject,
                IsBodyHtml = true,
                Body = body
            })
            {
                smtp.Send(message);
            }
        }

        entity.Name = customer.CustomerName;
        entity.TelephoneNumber = customer.PhoneNo;
        entity.AddressLine1 = customer.AddressLine1;
        entity.PostCode = customer.Postcode;
        entity.CompanyId = customer.CompanyId;
        entity.ServiceDate = customer.ServiceDate;
        entity.ServiceCompleteted = customer.ServiceCompletion;
        entity.BoilerName = customer.BoilerName;
        entity.BoilerAge = customer.BoilerAge;
        entity.ServiceRate = customer.ServiceRate;
        db.SaveChanges();
    }
}

1 个答案:

答案 0 :(得分:0)

我设法解决了问题,我使用了@Html.TextBoxFor,它给了我想要的东西。

@model PEF.IT.BoilerTest.Domain.CustomerModel
@{
    ViewBag.Title = "Add";
}
<h3><span style="text-decoration: underline;">Customer Information</span></h3>
<br />
@using (Html.BeginForm("SaveCustomer", "Companies"))
{
    @*@Html.EditorForModel()*@

    @Html.EditorFor(x => x.CompanyId, new { type = "hidden" })

    <div class="input-group">
        <span class="input-group-addon" style="width: 15em">Customer Name</span>
        @Html.TextBoxFor(x => x.CustomerName, new { @placeholder = "Mr Sam Smith", @class = "form-control", style = "width: 20em" })
    </div>
    <br />
    <div class="input-group">
        <span class="input-group-addon" style="width: 15em">Address Line 1</span>
        @Html.TextBoxFor(x => x.AddressLine1, new { @placeholder = "23 Greenbank Road", @class = "form-control", style = "width: 20em" })
    </div>
    <br />
    <div class="input-group col-sm-10">
        <span class="input-group-addon" style="width: 15em">Postcode</span>
        @Html.TextBoxFor(x => x.Postcode, new { placeholder = "BH1 1JU", @class = "form-control", style = "width: 20em" })
    </div>
    <br />
    <div class="input-group">
        <span class="input-group-addon" style="width: 15em">Phone Number</span>
        @Html.TextBoxFor(x => x.PhoneNo, new { placeholder = "01202 53689", @class = "form-control", style = "width: 20em" })
    </div>
    <br />
    <div class="input-group">
        <span class="input-group-addon" style="width: 15em">Boiler Name &amp; Type</span>
        @Html.TextBoxFor(x => x.BoilerName, new { placeholder = "Baxi, Gas", @class = "form-control", style = "width: 20em" })
    </div>
    <br />
    <div class="input-group">
        <span class="input-group-addon" style="width: 15em">Agoe Of Boiler</span>
        @Html.TextBoxFor(x => x.BoilerAge, new { placeholder = "2", @class = "form-control", style = "width: 20em" })
    </div>
    <br />
    <div class="input-group">
        <span class="input-group-addon" style="width: 15em">Service Request Date</span>
        @Html.TextBoxFor(x => x.ServiceDate, new { placeholder = "17/03/2015", @class = "form-control", style = "width: 20em" })
    </div>
    <br />
    <div class="input-group">
        <span class="input-group-addon" style="width: 15em">Agreed Amount £</span>
        @Html.TextBoxFor(x => x.ServiceRate, new { placeholder = "72.37", @class = "form-control", style = "width: 20em" })
    </div>
    <br/>
    <div class="col-sm-offset-2 col-sm-10">
        <button type="Submit" class="btn btn-primary">Save</button><span style="padding-left:2em "></span>
        @Html.ActionLink("Cancel", "Index", null, new { @class = "btn btn-warning" })
    </div>
}

感谢所有回复!