将部分绑定到多个操作结果

时间:2016-02-24 20:18:54

标签: c# asp.net-mvc

我想知道是否有人能提出克服以下问题的方法。我有以下型号

jobId

这就是我的控制器的样子

import java.util.UUID;

...

Dataproc dataproc = new Dataproc.Builder(new NetHttpTransport(), new JacksonFactory(), credential)
    .setApplicationName("my-webabb/1.0")
    .build();

String curJobId = "json-agg-job-" + UUID.randomUUID().toString();
Job jobSnapshot = null;
try {
  jobSnapshot = dataproc.projects().regions().jobs().submit(
      projectId, "global", new SubmitJobRequest()
          .setJob(new Job()
              .setReference(new JobReference()
                   .setJobId(curJobId))
              .setPlacement(new JobPlacement()
                  .setClusterName("my-spark-cluster"))
              .setSparkJob(new SparkJob()
                  .setMainClass("FooSparkJobMain")
                  .setJarFileUris(ImmutableList.of("gs://bucket/path/to/your/spark-job.jar"))
                  .setArgs(ImmutableList.of(
                      "arg1", "arg2", "arg3")))))
      .execute();
} catch (IOException ioe) {
  try {
    jobSnapshot = dataproc.projects().regions().jobs().get(
        projectId, "global", curJobId).execute();
    logger.info(ioe, "Despite exception, job was verified submitted");
  } catch (IOException ioe2) {
    // Handle differently; if it's a GoogleJsonResponseException you can inspect the error
    // code, and if it's a 404, then it means the job didn't get submitted; you can add retry
    // logic in that case.
  }
}

// We can poll on dataproc.projects().regions().jobs().get(...) until the job reports being
// completed or failed now.

两个视图看起来像这样

    // AModel
    public class AModel
    {
        public string PropOne { get; set; }
        public bool PropTwo { get; set; }
        public string Keyword { get; set; }
    }

    public class AModelViewModel
    {
        public AModelViewModel()
        {
            AModel = new AModel();
        }

        public AModel AModel { get; set; }
    }

    //BModel
    public class BModel
    {
        public string PropOne { get; set; }
        public bool PropTwo { get; set; }
        public string Keyword { get; set; }
    }

    public class BModelViewModel
    {
        public BModelViewModel()
        {
            BModel = new BModel();
        }

        public BModel BModel { get; set; }
    }

两个视图都使用以下部分视图

    public ActionResult PageA()
    {
        var model = new AModelViewModel();
        return View(model);
    }

    [HttpPost]
    public ActionResult PageA(AModel aModel)
    {
        return View();
    }

    public ActionResult PageB()
    {
        var model = new BModelViewModel();
        return View(model);
    }

    [HttpPost]
    public ActionResult PageB(BModel bModel)
    {
        return View();
    }

我遇到的问题是因为这个部分是共享的,并且关键字输入的name属性是'Keyword',当我在任一页面上提交表单时,关键字属性永远不会被绑定,所以它总是为空。有没有办法可以分享这个部分,但根据我正在使用的页面改变前缀。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

而不是使用paritial视图..在Views \ Shared \ EditorTemplates中创建一个名为Keyword.cshtml的EditorTemplate,其代码类似于

@model string
//_RandomView
<div class="form-group">
    <label for="keyword">Keyword</label>
    @Hmlt.TextBoxFor(a => a, new {class="form-control"})
</div>

然后在您的观看中,您只需致电

@Html.EditorFor(a => a.AModel.Keyword,"Keyword")

如果你必须使用PartialView ..你可以在调用局部视图时通过ViewDataDictionary传入HtmlFieldPrefix。要做到这一点,你可以这样称呼你的部分。

@Html.Partial("~/Views/Shared/_RandomView.cshtml", 
                 new ViewDataDictionary { TemplateInfo = new TemplateInfo() {
                       HtmlFieldPrefix = "AModel" } });

然后在你的局部视图中你需要像这样使用@ Html.Textbox助手。

<div class="form-group">
    <label for="keyword">Keyword</label>
    @Html.TextBox("Keyword", string.Empty, new { @class="form-control" })
</div>
相关问题