从视图中传递价值,控制器MVC [英] passing value from view to controller in MVC

查看:77
本文介绍了从视图中传递价值,控制器MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的看法。

 <形式方法=邮报行动=/ LoadCustomerAndDisplay /搜索>
<&字段集GT;
    <传奇>客户书< /传说>
    <%= Html.Label(姓名)%>    <%:Html.TextBox(姓名)%>
    < BR />
    < BR />
    < D​​IV>
        <输入类型=提交值=符号/>
    < / DIV>
< /字段集>
< /表及GT;

这是我的控制器...

 公众的ActionResult搜索()
    {
        CustomerModels objCustomer =新CustomerModels();
        变种dataval = objCustomer.getData();
        返回查看(dataval);}

我怎样才能获得的名称在控制器文本框的值,并将其传递给了的getData这样的....

  VAR dataval = objCustomer.getData(计算机['名称']);

这我把......显示在FNAME错误....添加缺失的指令....现在什么的问题...

 <%Html.BeginForm(搜索,LoadCustomerAndDisplay);%GT;
    &所述;%:Html.TextBoxFor(M => m.fname)%GT;
    &所述p为H.;
        <按钮式=提交>
            保存< /按钮>< / P>
    &所述;%Html.EndForm();%GT;


解决方案

使用强类型的视图。在您的GET操作方法,通过您的视图模型的目的是在查看和使用HTML辅助方法来创建输入元素。当您提交表单,由于MVC模式的结合,你会得到的值作为视图模型中的 POST 操作方法的属性值。

您GET操作可以保持相同的

 公众的ActionResult搜索()
{
    CustomerModels objCustomer =新CustomerModels();
    变种dataval = objCustomer.getData();
    //假设此方法返回CustomerViewModel对象
    //我们将传递到视图。    返回查看(dataval);
}

让你查看就会像

  @model CustomerViewModel
@using(Html.BeginForm())
{
  @ Html.LabelFor(X => x.Name)
  @ Html.TextBoxFor(X => x.Name)
  <输入类型=提交值=保存/>
}

和有发表的操作方法来处理这​​个

  [HttpPost]
公众的ActionResult搜索(CustomerViewModel模型)
{
  如果(ModelState.IsValid)
  {
    字符串名称= model.Name;   //你可以保存和重定向这里(PRG模式)
  }
  返回查看(模型);}

假设你的 objCustomer.getData()方法的GET操作方法返回的 CustomerViewModel 其中有一个对象名称属性像这样

 公共类CustomerViewModel
{
  公共字符串名称{集;获取;}
  //根据需要其它性能
}

This is my view

<form method="post" action="/LoadCustomerAndDisplay/Search">
<fieldset>
    <legend>Customer Book</legend>
    <%= Html.Label("Name") %>

    <%: Html.TextBox("Name") %>
    <br />
    <br />
    <div>
        <input type="submit" value="Sign" />
    </div>
</fieldset>
</form>

This is my controller...

 public ActionResult Search() 
    {
        CustomerModels objCustomer = new CustomerModels();
        var dataval = objCustomer.getData();
        return View(dataval);

}

How can i get the value of Name textbox in the controller and pass it to the the getData like this....

 var dataval = objCustomer.getData(ViewData['Name']);

this i put...showing error on fname....missing adding directive....what's the issue now...

 <% Html.BeginForm("Search", "LoadCustomerAndDisplay");%>
    <%: Html.TextBoxFor(m => m.fname) %>
    <p>
        <button type="submit">
            Save</button></p>
    <% Html.EndForm();%>

解决方案

Use strongly typed view. In your GET action method, pass an object of your ViewModel to the view and use the HTML helper methods to create the input elements. When you submit the form, due to MVC model binding, you will get the values as the property values of the ViewModel in the POST action method.

Your GET action can stay same

public ActionResult Search() 
{
    CustomerModels objCustomer = new CustomerModels();
    var dataval = objCustomer.getData(); 
    // Assuming this method returns the CustomerViewModel object 
    //and we will pass that to the view.

    return View(dataval);
}

so your View will be like

@model CustomerViewModel
@using (Html.BeginForm())
{
  @Html.LabelFor(x=>x.Name)
  @Html.TextBoxFor(x=>x.Name)
  <input type="submit" value="Save" /> 
}

And have a POST action method to handle this

[HttpPost]
public ActionResult Search(CustomerViewModel model)
{
  if(ModelState.IsValid)
  {
    string name= model.Name;

   //  you may save and redirect here (PRG pattern)
  }
  return View(model);

}

Assuming your objCustomer.getData() method in your GET Action method returns an object of CustomerViewModel which has a Name property like this

public class CustomerViewModel
{
  public string Name { set;get;}
  //other properties as needed
}

这篇关于从视图中传递价值,控制器MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆