无法将类型为'System.Data.Entity.DbSet` [City]'的对象强制转换为'System.Collections.Generic.IEnumerable`1 [System.Web.Mvc.SelectListItem]'

时间:2016-09-10 07:20:46

标签: c# asp.net-mvc-5

我正在尝试从城市表中加载所有城市并在客户端的下拉列表中显示它,但它显示此错误

  

无法转换类型为'System.Data.Entity.DbSet 1[EVS.Dotnet277.Entities.City]' to type 'System.Collections.Generic.IEnumerable 1 [System.Web.Mvc.SelectListItem]'的对象。

动作

[HttpGet]
public ActionResult SignUp()
{
    ViewBag.CityList = _userService.GetAllCities();
    return View();
}

HTML

<div class="col-md-6">
<label class="select margin-bottom-15">
@Html.DropDownList("City", (IEnumerable<SelectListItem>)ViewBag.CityList, "- Select City -", new { @class = "form-control"})
/label>
</div>

1 个答案:

答案 0 :(得分:1)

将您的代码更改为:

<?php

    $status=array();

    if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['contact-form-name'], $_POST['contact-form-email'], $_POST['contact-form-message'] ) ){

        $name = !empty( $_POST['contact-form-name'] ) ? $_POST['contact-form-name'] : false;
        $email = !empty( $_POST['contact-form-email'] ) ? filter_input('contact-form-email', FILTER_VALIDATE_EMAIL ) : false;
        $message = !empty( $_POST['contact-form-message'] ) ? $_POST['contact-form-message'] : false;


        if( !$name || !$email || !$message ){
            $status[]='one or more required fields are empty';
        }

        if( empty( $status ) ){
            /* email elements */
            $to = 'name@example.com';
            $subject = 'Subject';
            $body = "\n
                Name: {$name}\n\n
                Message: {$message}\n";
            $headers = "From: {$email}\r\n";

            /* send the email - assign result as a variable */
            $result=@mail( $to, $subject, $body, $headers );

            /* add a status based on mail sending */
            $status[]=$result ? 'Mail sent' : 'Mail could not be sent';




            if( $result ){

                $username = 'youremail@address.com';
                $hash = 'Your API hash';

                $numbers = array( 918123456789, 918987654321 );
                $sender = urlencode( 'TXTLCL' );
                $message = rawurlencode( $message );
                $numbers = implode( ',', $numbers );


                $data = array(
                    'username'  => $username,
                    'hash'      => $hash,
                    'numbers'   => $numbers,
                    'sender'    => $sender,
                    'message'   => $message
                );


                $ch = curl_init( 'http://api.textlocal.in/send/');
                curl_setopt( $ch, CURLOPT_POST, true );
                curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
                curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
                $response = curl_exec( $ch );
                curl_close( $ch );

                $status[]=$response;
            }
        }

    } else {
        $status[]='Invalid method';
    }

    /* process the various status messages stored in the array */
    echo "<h2>Status</h2><ul><li>", implode( "</li><li>", $status ), "</li></ul>";

?>

查看代码:

[HttpGet]
public ActionResult SignUp()
{
    ViewBag.CityList =new SelectList( _userService.GetAllCities(),dataValueField, dataTextField);
    return View();
}