使用btn中的onclick标记更改URL

时间:2013-09-09 09:16:18

标签: c# html5 asp.net-mvc-4

我在一个视图中使用了以下内容,以便导航到索引视图中的特定选项卡...

  button type="button" class="btn blue" onclick="location.href='Index/#rooms'"><i class="m-icon-swapleft m-icon-white"></i> Back to Room List</button>

网址读起来像..... ReferralTarget / Index /#rooms

房间是索引视图中的一个标签..

当我在另一个视图中使用相同的代码时,我收到错误400 - 错误的请求和Url读取的内容如... / ReferralTarget / EditRoom / Index /#rooms

   @model.Project.Web.Models.ReferralTargetRoomModel
  @{
  ViewBag.Title = "EditRoom";
  Layout = "~/Views/Shared/_Layout.cshtml";
 }
 <!-- BEGIN PAGE CONTENT-->
 <div class="row-fluid">
 <div class="span12">
 <!-- BEGIN SAMPLE FORM PORTLET-->   
 <div class="portlet box blue">
 <div class="portlet-title">
 <div class="caption"><i class="icon-hospital"></i>Edit Room Details</div>      
</div>
<div class="portlet-body form">
<!-- BEGIN FORM-->

               @using (Html.BeginForm("EditRoom", "ReferralTarget", FormMethod.Post,     new { @class="form-horizontal" }))
              {
              @Html.AntiForgeryToken()
              @Html.ValidationSummary(true)

             @Html.HiddenFor(model => model.ID)
             @Html.HiddenFor(model => model.ReferralTargetId)
    <div class="control-group">
    <label class="control-label">@Html.LabelFor(model => model.County)</label>
    <div class="controls">
    @Html.DropDownListFor(model => model.County.ID, new SelectList(Model.Counties, "ID", "Name"), new {@id="counties" })
            @Html.ValidationMessageFor(model => model.County)
    </div>                                  
            <div class="control-group">
    <label class="control-label">@Html.LabelFor(model => model.Fax)</label>
    <div class="controls">   
     @Html.EditorFor(model => model.Fax)
            @Html.ValidationMessageFor(model => model.Fax)
    </div>
    </div>
            <div class="control-group">
    <label class="control-label">@Html.LabelFor(model => model.Email)</label>
    <div class="controls">   
    @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
    </div>
    </div>
            <div class="control-group">
    <label class="control-label">@Html.LabelFor(model => model.Website)</label>
    <div class="controls">   
    @Html.EditorFor(model => model.Website)
            @Html.ValidationMessageFor(model => model.Website)
    </div>
</div>
            <div class="control-group">
    <label class="control-label">@Html.DisplayNameFor(model => model.Users)</label>
    <div class="controls">   
    @Html.ListBoxFor(m => m.SelectedUsers,  new MultiSelectList(Model.Users,"ID","Name",Model.SelectedUsers)
           , new { @id = "users"  , @multiple = true})
</div>
</div>                                      
<div class="form-actions">
   <input type="submit" value="Save Changes" class="btn green">
  <button type="button" class="btn blue" onclick="location.href='Index/#rooms'"><i    class="m-icon-swapleft m-icon-white"></i> Back to Room List</button>
    </div>
           }                                
    <!-- END FORM-->  
    </div>
</div>
     <!-- END SAMPLE FORM PORTLET-->
    </div>
    <!-- END PAGE CONTENT-->
                </div>
   @section Scripts {
   @Scripts.Render("~/bundles/jqueryval")
   @Scripts.Render("~/Scripts/jquery.multi-select.js")
    }
   @section Styles {
    <link href="~/assets/css/multi-select.css" rel="stylesheet" />
  }
 <script type="text/javascript">
 $(document).ready(function () {
 $(document).ready(function () {
  $('#users').multiSelect({
     selectableHeader: "<div class='custom-header'>Availible Users</div>",
     selectionHeader: "<div class='custom-header'>Selected Users</div>",
   });
   });
   $(function () {
    $("#counties").change(function () {
    var filterVal = $("#counties").val();
   getHospitalsByCounty(filterVal);
   })
  });
    function getHospitalsByCounty(value) {
    $.ajax({
     url: 'GetHospitalsInCounty?id=' + value,
     contentType: "application/json; charset=utf-8",
     success: function (response) {
     var data = response;
     var items = []
    $('#hospitals').empty();
   $.each(data, function (index, value) {
  $('#hospitals').append($('<option></option>').val(value.ID).html(value.Name));
   });
 }
});
 };
 });
 </script>

有没有办法改变网址来删除文字,在这种情况下是“编辑房间”.. ??

如果它与控制器有关,那么两个视图中唯一的区别就是EditView Get ActionResult使用了一个(字符串id),其中AddView没有....

由于

1 个答案:

答案 0 :(得分:0)

从您的代码中我认为您无意中添加了一个不存在的操作方法。

EditRoom是您想要的操作,还是Index您想要的操作。

我猜控制器是ReferralTarget

所以它应该是ReferralTarget/EditRoom/#rooms

<强>已更新

您可以更改此行。

 onclick="location.href='Index/#rooms'"

 onclick="location.href='@Url.Action("Index","ReferralTarget")/#rooms'"