简体   繁体   English

我们可以将整个 model 传递给 javascript asp.net mvc

[英]can we pass entire model to javascript asp.net mvc

I have a problem that on javascript call for the form submit, the model gets updated from controller,but it is not updating in the view.我有一个问题,在 javascript 调用表单提交时,model 从 controller 更新,但它没有在视图中更新。 I am thinking to update the model to new model values in javascript.我正在考虑将 model 更新为 javascript 中的新 model 值。 so that the view shows the latest model values can that be done?这样视图可以显示最新的 model 值吗?

thanks, michael谢谢,迈克尔

Your question is extremely unclear and you provided no source code which makes things even more unclear.您的问题非常不清楚,并且您没有提供任何源代码,这使事情变得更加不清楚。 From the various comments you may have posted I assume that you are trying to update some model value inside the POST action without removing it from the model state and when the same view is rendered again the old values are displayed.从您可能发布的各种评论中,我假设您正在尝试更新 POST 操作中的一些 model 值,而不将其从 model state 中删除,并且再次显示旧值时。

So I suppose you have a view model that looks something close to this:所以我想你有一个看起来接近这个的视图 model:

public class MyViewModel
{
    public HttpPostedFileBase File { get; set; }
    public string SomeValue { get; set; }
}

and a controller:和 controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            SomeValue = "initial value"
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        // Notice how the SomeValue property is removed from the
        // model state because we are updating its value and so that
        // html helpers don't use the old value
        ModelState.Remove("SomeValue");
        model.SomeValue = "some new value";
        return View(model);
    }
}

and a view:和一个观点:

<% using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" })) { %>
    <div>
        <%= Html.LabelFor(x => x.SomeValue) %>
        <%= Html.EditorFor(x => x.SomeValue) %>
    </div>
    <div>
        <label for="file">Attachment</label>
        <input type="file" name="file" />
    </div>
    <input type="submit" value="OK" />
<% } %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM