2

Im new to ASP.net MVC and I use the DevExpress-MVC extensions to build a small application, a time registration for employees, that uses a DataGrid.

My Problem is the following one:

The user is able to enter a abbreviation of his name, submitting this to the controller should call a query to my DB and show his responding Employee-ID.

I got my DataViewModel to pass my Model to the view, now I thought binding it to the textbox and retrieving the data back from the ViewModel would do it, but it does not.

My Code looks like that:

ViewModel

    public class MyErfassungViewModel
{
    public MyErfassung erfassung;
    public String personalnummer;
    public String personalkuerzel;
    public List<String> dim1;
    public List<String> dim2;
    public List<String> dim3;
    public List<String> dim4;
    public List<String> dim5;

    public MyErfassungViewModel(MyErfassung myerfassung)
    {
        this.erfassung = myerfassung;
    }
}

View

    @model DevExpressMvcApplication.Models.ViewModel.MyErfassungViewModel
    <div class ="Personalnummer">
    @using (Html.BeginForm("getNummer","MyErfassung"))
    {
        <label>Personalkürzel</label>
        @Html.DevExpress().TextBox(settings =>
        {
            settings.Name = "TextBoxPersonalkürzel";
        }).Bind(Model.personalkuerzel).GetHtml()

        <label>Personalnummer</label>

        @Html.DevExpress().TextBox(settings =>
        {
            settings.Name = "TextBoxPersonalnummer";
            settings.ReadOnly = true;
        }).Bind(Model.personalnummer).GetHtml()

        @Html.DevExpress().Button(settings =>
        {
            settings.Name = "TextBoxKuerzel";
            settings.Text = "Holen";
            settings.UseSubmitBehavior = true;
        }).GetHtml()
        <br>
    }
</div>

Controller

    //
    // GET: /MyErfassung/

    public ActionResult Index()
    {
        viewModel.personalkuerzel = "sese";
        return View(viewModel);
    }
    [HttpPost]
    public ActionResult getNummer()
    {
        String kurz = Convert.ToString(Request["TextBoxKuerzel"]);
        String nr = Convert.ToString(Request["TextBoxPersonalnummer"]);

        String nummer = dbRepo.getPersonalnummerByKuerzel(viewModel.personalkuerzel);
        viewModel.personalnummer = nummer;
        return View(viewModel);
    }

Like you are able to see I tried some possible ways to receive the user input and send a DB-Request to get a number. But either the request or the viewmodel are containing more than a null reference.

But simply said I am not able to pass users input from my form back to the controller.

4

2 回答 2

0

您只需将视图模型传递给您的控制器操作,模型绑定器将负责其余的工作。然后,您可以访问这些属性并按照您认为合适的方式使用它们:

[HttpPost]
public ActionResult getNummer(MyErfassungViewModel viewModel)
{
    String nummer = dbRepo.getPersonalnummerByKuerzel(viewModel.personalkuerzel);
    ...
}

当您为 POST 操作方法定义一个MyErfassungViewModel参数(例如,称为viewModel)时,ASP.NET MVC 模型绑定器将采用来自您的视图的表单值,填充一个实例MyErfassingViewModel并将其传递给您的方法。

目前,你的getNummer方法的参数列表是空的,所以没有地方可以传入视图模型。

更新:因为MyErfassungViewModel有一个重载的构造函数,你需要添加一个默认构造函数,以便模型绑定器可以实例化它:

public class MyErfassungViewModel
{
    public MyErfassung erfassung;
    public String personalnummer;
    public String personalkuerzel;
    public List<String> dim1;
    public List<String> dim2;
    public List<String> dim3;
    public List<String> dim4;
    public List<String> dim5;

    public MyErfassungViewModel(MyErfassung myerfassung)
    {
        this.erfassung = myerfassung;
    }

    public MyErfassungViewModel()
    {
    }
}
于 2013-04-29T10:58:11.570 回答
0

以下问题中提出了最终将我引向我的数据的解决方案

ASP.NET MVC 将数据从视图传递到控制器

对于我的解决方案,代码如下所示:

查看

    [HttpPost]
    public ActionResult getNummer()
    {
        String kurz = Convert.ToString(Request["personalkuerzel"]);
        String nr = Convert.ToString(Request["personalnummer"]);

        String nummer = dbRepo.getPersonalnummerByKuerzel(kurz);
        viewModel.personalnummer = nummer;
        return View(viewModel);
    }

不幸的是,这个解决方案似乎没有使用 Ant P 描述的舒适方式,即自动将响应数据应用于模型。

于 2013-04-29T12:37:08.610 回答