ASP.NET MVC 中使用 Vue 实现跨页面值传递 (POST 请求)
可以通过在 Vue 中使用 axios 库向后端发送 POST 请求,并将 Vue 中的值作为请求的参数传递给 MVC 控制器。
以下是一个简单的示例:
Vue 组件中的 axios 请求:
import axios from 'axios';
export default {
data() {
return {
inputValue: ''
};
},
methods: {
submitForm() {
axios.post('/Controller/Action', {
inputValue: this.inputValue
}).then(response => {
// 处理响应
}).catch(error => {
// 处理错误
});
}
}
}
MVC 控制器中的 Action 方法:
[HttpPost]
public ActionResult Action(string inputValue)
{
// 处理传递过来的值
return View();
}
在上述示例中,axios.post() 方法向 MVC 控制器发送 POST 请求,请求的 URL 为 '/Controller/Action'。请求的参数是一个包含 Vue 组件中的 inputValue 值的对象。MVC 控制器中的 Action 方法接收这个参数,并将其作为字符串类型的 inputValue 参数进行处理。在实际应用中,可以根据需要进行参数的类型转换、验证等处理。
原文地址: https://www.cveoy.top/t/topic/o0bj 著作权归作者所有。请勿转载和采集!