文本框数据写回数据库,但textboxfor不会

时间:2015-07-08 01:43:12

标签: c# .net model-view-controller html-helper

我认为使用强类型文本框来辅助方法而不是简单的文本框是最佳做法。我全力以赴。但是当我在项目中进行切换时,数据停止写回数据库。我无法弄清楚原因。

这是我的模特。

#include <iostream>
#include <string>
#include <CGAL/Cartesian.h>
#include <CGAL/Filtered_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
typedef double Real;
typedef CGAL::Cartesian<Real> Kernel0;
// Use a filtered kernel so that all predicates are exact.
typedef CGAL::Filtered_kernel<Kernel0> Kernel;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron;
typedef Kernel::Point_3 Point;

void Edge_Analysis(Polyhedron mesh){
  float mean = 0, min, max, length;
  int count = 0; bool init = true;
  for (Polyhedron::Edge_const_iterator edgeIter = mesh.edges_begin(); edgeIter != mesh.edges_end(); ++edgeIter){

      Point a = edgeIter.prev()->vertex()->point();
      Point b = edgeIter.vertex()->point();
      length = CGAL::sqrt(CGAL::squared_distance(a, b));
      ++count;
      if (init){
          mean = min = max = length;
          init = false;
      }
      else{
          if (length < min) min = length;
          if (length > max) max = length;
      }
      mean += length;
  }
  mean /= count;
  std::cout << min << " " << max << " " << mean << "\n";
}

int main(int argc, char **argv){

  Polyhedron mesh;

  // Read the input mesh from standard input in OFF format.
  if (!(std::cin >> mesh)) {
    std::cerr << "Cannot read input mesh\n";
    return 1;
  }
  Edge_Analysis(mesh);
return 0;
}

然后控制器

public class MasterModel
{
    [Key]
    public int mandatoryKey { get; set; }
    public List<tblAddress> Address { get; set; }
    public List<tblPrimaryCaregiverdata> Primary { get; set; }
    public List<tblPhone> Phone { get; set; }
    public List<tblEmail> Email { get; set; }
    public List<tblRelatedCaregiver> Related { get; set; }
    public List<tblTrainingHistoryMain> TrainingHistory { get; set; }
    public List<tblInquiryReferralStatu> InquiryReferral { get; set; }
}

现在,在视图中(调用几个局部视图,主模型中每个表一个),当我创建这样的字段时:

public ActionResult Create(MasterModel masterModel)
    {
        if (ModelState.IsValid)
        {

            db.tblPrimaryCaregiverdatas.Add(masterModel.Primary[0]);
            db.SaveChanges();

            int newCareGiverID = db.tblPrimaryCaregiverdatas.OrderByDescending(p => p.CareGiverID)
                .FirstOrDefault().CareGiverID;


            foreach (var ph in masterModel.Phone)
            {
                ph.CareGiverID = newCareGiverID;
                db.tblPhones.Add(ph);
            }

            db.SaveChanges();
            return RedirectToAction("Index");
        }

...我能够将数据写回数据库。但当我切换到文本框时,这样:

SelectList phonetypes = ViewBag.PhoneType;
<div>
    <label class="label-fixed-width">Phone:</label>
    @Html.TextBox("masterModel.Phone[0].phone", null, new { style = "width: 600px" })
    @Html.DropDownList("masterModel.Phone[0].PhoneType",phonetypes, null, new
    {
        @class = "form-control-inline dropdown",
        style = "width: 100px"
    })

当我回发时,MasterModel的Phone部分为空。有人可以帮我理解这里发生了什么吗?

1 个答案:

答案 0 :(得分:0)

将模型发布回ActionResult并返回相同的View时,模型对象的值包含在ModelState中。 ModelState包含有关有效/无效字段以及实际POSTed值的信息。如果要更新模型值,可以执行以下操作:

 foreach (var ph in masterModel.Phone)
            {
               ModelState.Clear()//added here
                ph.CareGiverID = newCareGiverID;
                db.tblPhones.Add(ph);
            }