2014-10-18 25 views
-1

在我_Layout.cshml文件,我有以下代码段:如何使用MVC5中的数据重定向到新视图?

<div class="input-top"> 
    <a class="GoBtn" href=""><img src="~/Content/img/GOBtn.png" class="gobtn-position"></a> 
    <input id="homeZipCode" type="text" class="form-control input-position-2" placeholder="ZIP"> 
    <input id="homeService" type="text" class="form-control input-position-1" placeholder="What do you need done today?"> 
</div> 

而且一点是,我想上面的两个输入字段的值,如果href重定向到一个新观点属性被点击,但是当它重定向并打开新视图时,在新视图中,在特定的两个输入字段而不是空白处,我希望这些数据被呈现,我的意思是要写入的文本。在我重定向观点,我有这样的事情:

<div class="form-group"> 
          @Html.LabelFor(model => model.ZipCode, "Zip", htmlAttributes: new { @class = "control-label col-md-2" }) 
          <div class="col-md-10"> 
           @Html.EditorFor(model => model.ZipCode, new { htmlAttributes = new { @class = "form-control" } }) 
           @Html.ValidationMessageFor(model => model.ZipCode, "", new { @class = "text-danger" }) 
          </div> 
         </div> 
        <div class="form-group"> 
         @Html.LabelFor(model => model.ServiceName, "Service", htmlAttributes: new { @class = "control-label col-md-2" }) 
         <div class="col-md-10"> 
          @Html.EditorFor(model => model.ServiceName, new { htmlAttributes = new { @class = "form-control", @id = "service-manual" } }) 
          @Html.ValidationMessageFor(model => model.ServiceName, "", new { @class = "text-danger" }) 
         </div> 
        </div> 

出于这个原因,我试着写一个AJAX调用,并把它放在我的_Layout.cshtml文件,但它似乎不工作。下面你可以找到我的AJAX调用:

$(document).ready(function() { 
    $('a.GoBtn').on('click', function (e) { 

     e.preventDefault(); 

     var homeZipCode = $("#homeZipCode").val(); 
     var homeService = $("#homeService").val(); 

     var model = { StateID: 1, ZipCode: homeZipCode, ServiceName: homeService }; 

     $.ajax({ 
      url: '@Url.Action("ServiceRequest", "Home")', 
      contentType: 'application/json; charset=utf-8', 
      type: 'POST', 
      dataType: 'html', 
      data: JSON.stringify(model) 
     }) 
      .success(function (result) { 
      }); 
    }); 
}); 

的问题是,当我点击href属性,视图不会重定向,我还是那个我点击HREF的看法。可能是什么问题?

编辑:

public class RequestViewModel 
{ 
    [Required(ErrorMessage = "Please select a state")] 
    [Display(Name = "State")] 
    public int StateID { get; set; } 

    [Required(ErrorMessage = "Please enter a zip code")] 
    [Display(Name = "Zip")] 
    public string ZipCode { get; set; } 

    [Required(ErrorMessage = "Please choose a service")] 
    [Display(Name = "Service")] 
    public string ServiceName { get; set; } 
} 

而且我的控制器:

[Authorize] 
    public ActionResult ServiceRequest() 
    { 
     ViewBag.StateID = new SelectList(db.States, "StateID", "StateName"); 
     return View(); 
    } 

    [Authorize] 
    [HttpPost] 
    public ActionResult ServiceRequest(RequestViewModel rvm, HttpPostedFileBase image = null, HttpPostedFileBase video = null) 
    { ... } 

回答

1

你有e.preventDefault();这是防止点击一个链接的默认行为,您的点击事件里面。

而对于您的用例,您不需要使用ajax发布。你可以把你的输入控件放在一个表单标签中并保留一个提交按钮。如果你仍然想要链接,而不是提交按钮,当用户点击链接按钮时,你可以写一点javascript来发布表单。

而在服务器端,有一个操作方法接受你的表单内容,然后重定向到一个新的视图。您可以创建一个视图模型并设置它的属性并将其发送到您的新视图。

@using(Html.Beginform("Service","Home")) 
{ 
    <div class="input-top"> 
    <a class="GoBtn" href=""><img src="~/Content/img/GOBtn.png"></a> 
    <input name="ZipCode" type="text" class="form-control input-position-2" > 
    <input name="HomeService" type="text" class="form-control input-position-1" > 
    <input type="submit" value="Go" /> 
    </div> 
} 

和你的服务器端代码会

public ActionResult Service(ServiceRequestViewModel model) 
{ 
    return View(model); 
} 

假设你有一个像这样

public class ServiceRequestViewModel 
{ 
    public string ZipCode {set;get;} 
    public string HomeService{set;get;} 
} 

现在你的第二个视图查看模型,service.cshtm L将是强类型我们查看模型

@model ServiceRequestViewModel 
<div> 
    <p> @Html.TextBoxFor(s=>s.ZipCode) <p> 
    <p> @Html.TextBoxFor(s=>s.HomeService) <p> 
</div> 
+0

我的输入位于我的_Layout.cshtml文件中,我想重定向到实际使用模型的不同视图。 – tett 2014-10-18 22:53:34

0

你要先后缠上你输入字段的表单标签,如果你想要把数据上传到您的控制器操作,以便您的_Layout.cshtml代码需要改变有些

@using(Html.BeginForm("ServiceRequest","Home",FormMethod.Post,htmlAttributes:new{id="servicerequest-form"})){ 
    <div class="input-top"> 
     <a class="GoBtn" href=""><img src="~/Content/img/GOBtn.png" class="gobtn-position"></a> 
     <input id="ZipCode" name="rvm.ZipCode" type="text" class="form-control input-position-2" placeholder="ZIP"> 
     <input id="ServiceName" name="rvm.ServiceName" type="text" class="form-control input-position-1" placeholder="What do you need done today?"> 
    </div> 
} 

然后在你的JavaScript可以在标签的点击事件上发帖。

$(document).ready(function() { 
    $('a.GoBtn').on('click', function (e) { 

     e.preventDefault(); 

     var servicerequestForm = $("#servicerequest-form); 
     servicerequestForm.submit(); 

    }); 
}); 

现在是行动的实施,应该是这个样子

[HttpPost] 
public ActionResult ServiceRequest(RequestViewModel rvm, HttpPostedFileBase image = null, HttpPostedFileBase video = null) 
{ 
    return View(rvm); 
} 

假设你的新观点被称为“ServiceRequest.cshtml”。控制器中似乎还有很多事情要做,而且这些观点可能会影响答案。例如

 ViewBag.StateID = new SelectList(db.States, "StateID", "StateName"); 

这看起来像正在被填充,但似乎并不在你给_Layouts.cshtml中反映的下拉列表。同时将文件发布到一起将模型信息也给出了不同的答案,因为如何处理该问题,我希望如果有所帮助,我可以有所帮助?

+0

这是重定向,但在我的新视图中,与我的模型相对应的输入字段为空,它们没有在我的Layout中的输入字段中提供的文本。 – tett 2014-10-18 22:51:32

+0

可以包含您用于模型和控制器的代码吗?这听起来像你没有为新视图设置模型 – 2014-10-18 22:55:27

+0

我做了一个编辑,检查出来。另请注意,最初,我在我的_Layout.cshml文件中,它没有任何模型。然后,我想重定向到具有RequestViewModel的视图。 – tett 2014-10-18 23:26:07

相关问题