从MVC中的存储过程返回多个列

时间:2021-12-09 16:38:06

Was previously using this line in my controler to return an id and text column from, a stored procedfure in asp.net MVC

以前在我的控制器中使用这一行来返回一个id和text列,来自asp.net MVC中的存储过程

user = new SelectList(ctx.Database.SqlQuery<LkUpGenderModel>("EXEC dbo.uspGetLkUpGender").ToList(), "GenderID", "Gender");

However i now want to return more text values, I have extended the model to have these extra fields but I'm getting an error.

但是我现在想要返回更多的文本值,我已经扩展了模型以获得这些额外的字段,但是我收到了一个错误。

Is there a way to get this working:

有没有办法让这个工作:

user = new SelectList(ctx.Database.SqlQuery<LkUpGenderModel>("EXEC dbo.uspGetLkUpGender").ToList(), "GenderID", "Gender", "GenderShort", "GenderCombined");

currently it flags the SelectList( saying the call is ambiguous

目前它标记了SelectList(说调用是不明确的

1 个解决方案

#1


The previous code simply returns instances of LkUpGenderModel and then builds a SelectList from that using the GenderID property of that class as the value and the Gender property as the display text.

前面的代码只返回LkUpGenderModel的实例,然后使用该类的GenderID属性作为值并将Gender属性作为显示文本构建一个SelectList。

Your new code doesn't request more properties, it simply passes additional parameters to the SelectList constructor: namely, dataGroupField which you're setting to the GenderShort property, and selectedValue, which you're setting to the GenderCombined property. See: https://msdn.microsoft.com/en-us/library/dn725507(v=vs.118).aspx.

您的新代码不会请求更多属性,它只是将其他参数传递给SelectList构造函数:即,您设置为GenderShort属性的dataGroupField,以及您设置为GenderCombined属性的selectedValue。请参阅:https://msdn.microsoft.com/en-us/library/dn725507(v = vs.118).aspx。

If you have additional columns being returned from the stored procedure that you want filled in on the class, then you should add additional properties to the class to handle those. However, since all you're doing with the data is creating a SelectList from it, you can't send any additional data to the view other than the value and display text, making returning additional data pointless in this instance.

如果要从要在类上填充的存储过程返回其他列,则应向该类添加其他属性以处理这些列。但是,由于您对数据执行的操作都是从中创建SelectList,因此除了值和显示文本之外,您无法向视图发送任何其他数据,从而使得在此实例中返回其他数据毫无意义。

#1


The previous code simply returns instances of LkUpGenderModel and then builds a SelectList from that using the GenderID property of that class as the value and the Gender property as the display text.

前面的代码只返回LkUpGenderModel的实例,然后使用该类的GenderID属性作为值并将Gender属性作为显示文本构建一个SelectList。

Your new code doesn't request more properties, it simply passes additional parameters to the SelectList constructor: namely, dataGroupField which you're setting to the GenderShort property, and selectedValue, which you're setting to the GenderCombined property. See: https://msdn.microsoft.com/en-us/library/dn725507(v=vs.118).aspx.

您的新代码不会请求更多属性,它只是将其他参数传递给SelectList构造函数:即,您设置为GenderShort属性的dataGroupField,以及您设置为GenderCombined属性的selectedValue。请参阅:https://msdn.microsoft.com/en-us/library/dn725507(v = vs.118).aspx。

If you have additional columns being returned from the stored procedure that you want filled in on the class, then you should add additional properties to the class to handle those. However, since all you're doing with the data is creating a SelectList from it, you can't send any additional data to the view other than the value and display text, making returning additional data pointless in this instance.

如果要从要在类上填充的存储过程返回其他列,则应向该类添加其他属性以处理这些列。但是,由于您对数据执行的操作都是从中创建SelectList,因此除了值和显示文本之外,您无法向视图发送任何其他数据,从而使得在此实例中返回其他数据毫无意义。