如何将值从textBox传递到POST

时间:2015-12-10 18:15:21

标签: c# asp.net-mvc

在我看来,我有:

@model DataAccess.AdeccoView

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>AdeccoView</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.EmployeeID, "EmployeeID", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("EmployeeID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.EmployeeID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Client.ClientName, "ClientName", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Client.ClientName, new { htmlAttributes = new { @class = "form-control" } })
                @*@Html.EditorForModel(model => model.Client.ClientName, new { htmlAttributes = new { @class = "form-control" } }))*@

                @Html.ValidationMessageFor(model => model.Client.ClientName, "upisi me!", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.VisitTypeID, "VisitTypeID", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("VisitTypeID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.VisitTypeID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ResultTypeID, "ResultTypeID", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("ResultTypeID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.ResultTypeID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

对于EditorFor助手,我想在我的控制器ClientName中传递POST方法。

首先,在GET方法中,我尝试制作新的ViewBag或通过新实例进行调用。

public ActionResult Create()
{
    ViewBag.Client = new SelectList(db.Client, "ClientName");//I've added this
    ViewBag.ClientID = new SelectList(db.Client, "ClientID", "ClientName");
    return View();
}

接下来,在我的POST方法中,我有:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ViewID,EmployeeID,Date,VisitTypeID,ResultTypeID,ClientName, ClientID")] AdeccoView adeccoView)
{
    Client client = new Client();
    AdeccoView view = new AdeccoView();
    string a = view.Client.ClientName;//here is my code throw an error

    if (ModelState.IsValid)
    {
        db.AdeccoView.Add(adeccoView);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    ViewBag.Klijent = new SelectList(db.Client, "ClientName");
    ViewBag.ClientID = new SelectList(db.Client, "ClientID", "ClientName", adeccoView.ClientID);
    ViewBag.EmployeeID = new SelectList(db.Employee, "EmployeeD", "Name", adeccoView.EmployeeID);
    ViewBag.ResultTypeID = new SelectList(db.ResultType, "ResultTypeID", "ResultName", adeccoView.ResultTypeID);
    ViewBag.VisitTypeID = new SelectList(db.VisitType, "VisitTypeID", "VisitTypeName", adeccoView.VisitTypeID);
    return View(adeccoView);
}

我可能会将EditorForEditorForModelTextBox放在一起吗? 感谢。

编辑:使用TextBox帮助,我已经设法通过post方法传递Request.Form[],但css现在是一个棘手的问题。

2 个答案:

答案 0 :(得分:1)

editorFor和textbox之间没有区别。这里唯一重要的是名字。 您使用client.ClientName,但您在操作输入中只声明ClientName 我希望你能得到完整的想法

答案 1 :(得分:1)

您应该为视图创建一个平面视图模型,并使用它来在视图和操作方法之间传输数据。您可以添加属性来存储下拉列表的数据,以便我们可以避免使用ViewBag等动态内容。

public class CreateAdeccoView
{
   public int EmployeeID {set;get;}
   public int ClientID  {set;get;}
   public List<SelectListItem> Employees {set;get;}
   public List<SelectListItme> Clients {set;get;}
   public DateTime CreateDate {set;get;}
   //Add other properties as needed

}

在您的GET操作中,您创建了一个此对象,加载集合属性并发送到视图。

public ActionResult Create()
{
  var vm = new CreateAdeccoView();
  vm.Clients = new SelectList(db.Client, "ClientID", "ClientName");
  vm.Employees = new SelectList(db.Employees, "EmployeeID", "EmployeeName");

  return View(vm);
}

并在您的视图中

@model CreateAdecooView
@using(Html.BeginForm())
{
  <label>Employee </label>
  @Html.DropDownListfor(s=>s.EmployeeID,Model.Employees,"Select one")

  <label>Client</label>
  @Html.DropDownListfor(s=>s.ClientID,Model.Clients ,"Select one")

  <label>Date </label>
  @Html.TextBoxFor(s=>s.CreateDate)

  <input type="submit"/ >
}

在你的HttpPost行动中。

[HttpPost]
public ActionResult Create(CreateAdeccoView model)
{
  if(ModelState.IsValid)
  {
      AdeccoView view = new AdeccoView();
      view.EmployeeId=model.EmployeeID;
      view.ClientID=model.ClientID;
      //Don't forget to map other properties as well.

     db.AdeccoView.Add(adeccoView);
     db.SaveChanges();
     return RedirectToAction("Index");

  }   
  //Reload the data for dropdown again    
  model.Clients = new SelectList(db.Client, "ClientID", "ClientName");
  model.Employees = new SelectList(db.Employees, "EmployeeID", "EmployeeName");
  return View(model);
}