如何通过传递ActionLink的价值型 [英] How to pass Model Value through ActionLink

查看:110
本文介绍了如何通过传递ActionLink的价值型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的模型,对车辆的实体。
它具有属性:
模型
使
颜色
RegistrationPlate

I have a simple model which models a vehicle entity. It has attributes: Model Make Colour RegistrationPlate

在创建视图我希望用户能够执行服务器查找使用车牌自动填补模型/制作/彩色信息。

On the create View I want to users to be able to perform a server look up using the registration plate to auto fill the Model/Make/Colour information.

因此​​,我想创建这样一个动作链接:

As such I'm trying to create an action link like so:

@Html.ActionLink("Look up registration", "RegLookup", "Vehicle", Model.Registration)

在哪里RegLookup是在车辆控制器,它就会消失,并认为该模型/制作/颜色基础上,通过它传递的注册信息领域的一个get方法的ActionResult。在制作/模型/彩色信息从一个单独的数据库中检索。

Where RegLookup is a get ActionResult method in the vehicle controller which goes away and finds the model/make/colour information based on the registration field passed through to it. The Make/Model/Colour information is retrieved from a separate database.

然而,在渲染视图present - 即使获得的动作链接之前 - 抛出一个异常,表明该型号为NULL

However at present on rendering the view - before even getting to the action link - an exception is thrown indicating that the Model is Null.

我想通过传递有效地从文本重新presenting登记值:

What I want to be passed through is effectively the value from the textbox representing the registration:

@Html.EditorFor(model => model.Registration)

我如何通过传递这个值?

How do I pass this value through?

推荐答案

在您的CreateView的,通常你的模型属性应该大多是除了像下拉菜单一些prepoulated值空。你应该用的是使用jQuery AJAX进行异步调用返回基于车牌的相关数据的操作方法,并填写在表单

In your CreateView, typically your Model Properties should mostly be empty except some prepoulated values like dropdowns . What you should use is to use jQuery ajax to make an async call to an action method which returns the relevant data based on the registration plate and fill that in the Form

您可以保留这样的操作链接

You may keep the action link like this

@Html.ActionLink("Look up registration", "RegLookup", "Vehicle",
                                                      new {@id="check"},null)

和附加JavaScript函数它的单击事件进行Ajax调用

And attach a javascript function to the click event of it to make the ajax call

$(function(){

  $("#check").click(function(e){
    e.preventDefault();
    var item=$(this);
    var regPlateVal=$("#RegistrationPlate").val();
    $.getJSON(item.attr("href")+"?registration="+regPlateVal,function(data){
       if(data.Status=="success")
       {
         $("#Color").val(data.Color);
         $("#Model").val(data.Model);
       }
    });
  });

});

假设你的 RegLookUp 操作方法接受一个参数(与名称注册),并在返回数据 JSON 格式像下面的格式

Assuming your RegLookUp action method accept a parameter (With name registration) and returns the data in JSON format like below format

{
  "Status": "success",
   "Color": "Red",
   "Model": "2011"
}

这篇关于如何通过传递ActionLink的价值型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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